Esempio n. 1
0
 private String readEntityAsString(HttpEntity entity) throws IOException {
   if (entity == null) {
     throw new IOException("HTTP entity may not be null");
   }
   InputStream instream = entity.getContent();
   if (instream == null) {
     return null;
   }
   try {
     if (entity.getContentLength() > Integer.MAX_VALUE) {
       throw new IOException("HTTP entity too large");
     }
     int i = (int) entity.getContentLength();
     if (i < 0) {
       i = 4096;
     }
     if (entity.getContentEncoding() != null
         && entity.getContentEncoding().getValue().equals("gzip")) {
       instream = new GZIPInputStream(instream);
     }
     Reader reader = new InputStreamReader(instream, HTTP.UTF_8);
     CharArrayBuffer buffer = new CharArrayBuffer(i);
     char[] tmp = new char[1024];
     int l;
     while ((l = reader.read(tmp)) != -1) {
       buffer.append(tmp, 0, l);
     }
     return buffer.toString();
   } finally {
     instream.close();
   }
 }
Esempio n. 2
0
  /** HttpClient 4.3 简单入门---HttpGet */
  @Test
  public void test01() {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // 创建httpClientBuilder
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); // 创建httpClient
    HttpGet httpGet = new HttpGet(HTTPGETTESTURL);
    logger.info("httpGet.getRequestLine():{}", httpGet.getRequestLine());

    try {
      HttpResponse httpResponse = closeableHttpClient.execute(httpGet); // 执行get请求
      HttpEntity httpEntity = httpResponse.getEntity(); // 获取响应消息实体
      logger.info("响应状态:{}", httpResponse.getStatusLine());
      if (httpEntity != null) {
        logger.info("contentEncoding:{}", httpEntity.getContentEncoding());
        logger.info("response content:{}", EntityUtils.toString(httpEntity));
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        closeableHttpClient.close(); // 关闭流并释放资源
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  /**
   * 将Entity的内容载入Page对象
   *
   * @date 2013-1-7 上午11:22:06
   * @param entity
   * @return
   * @throws Exception
   */
  private Page load(HttpEntity entity) throws Exception {
    Page page = new Page();

    // 设置返回内容的ContentType
    String contentType = null;
    Header type = entity.getContentType();
    if (type != null) contentType = type.getValue();
    page.setContentType(contentType);

    // 设置返回内容的字符编码
    String contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) contentEncoding = encoding.getValue();
    page.setEncoding(contentEncoding);

    // 设置返回内容的字符集
    String contentCharset = EntityUtils.getContentCharSet(entity);
    page.setCharset(contentCharset);
    // 根据配置文件设置的字符集参数进行内容二进制话
    String charset = config.getCharset();
    String content = this.read(entity.getContent(), charset);
    page.setContent(content);
    //		if (charset == null || charset.trim().length() == 0)
    //			page.setContentData(content.getBytes());
    //		else
    //			page.setContentData(content.getBytes(charset));

    return page;
  }
Esempio n. 4
0
  // 底层请求打印页面
  private byte[] sendRequest(HttpRequestBase request) throws Exception {
    CloseableHttpResponse response = httpclient.execute(request);

    // 设置超时
    setTimeOut(request, 5000);

    // 获取返回的状态列表
    StatusLine statusLine = response.getStatusLine();
    System.out.println("StatusLine : " + statusLine);

    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
      try {
        // 获取response entity
        HttpEntity entity = response.getEntity();
        System.out.println("getContentType : " + entity.getContentType().getValue());
        System.out.println("getContentEncoding : " + entity.getContentEncoding().getValue());

        // 读取响应内容
        byte[] responseBody = EntityUtils.toByteArray(entity);

        // 关闭响应流
        EntityUtils.consume(entity);
        return responseBody;
      } finally {
        response.close();
      }
    }
    return null;
  }
Esempio n. 5
0
  /** Reads the contents of HttpEntity into a byte[]. */
  private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
        new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
      InputStream in = entity.getContent();
      Header contentEncoding = entity.getContentEncoding();
      if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(in);
      }

      if (in == null) {
        throw new ServerError();
      }
      buffer = mPool.getBuf(1024);
      int count;
      while ((count = in.read(buffer)) != -1) {
        bytes.write(buffer, 0, count);
      }
      return bytes.toByteArray();
    } finally {
      try {
        // Close the InputStream and release the resources by "consuming the content".
        entity.consumeContent();
      } catch (IOException e) {
        // This can happen if there was an exception above that left the entity in
        // an invalid state.
        VolleyLog.v("Error occured when calling consumingContent");
      }
      mPool.returnBuf(buffer);
      bytes.close();
    }
  }
Esempio n. 6
0
  private static Request transformRequest(HttpRequest request, Object tag) {
    Request.Builder builder = new Request.Builder();

    RequestLine requestLine = request.getRequestLine();
    String method = requestLine.getMethod();
    builder.url(requestLine.getUri());

    String contentType = null;
    for (Header header : request.getAllHeaders()) {
      String name = header.getName();
      if ("Content-Type".equals(name)) {
        contentType = header.getValue();
      } else {
        builder.header(name, header.getValue());
      }
    }

    RequestBody body = null;
    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
      if (entity != null) {
        // Wrap the entity in a custom Body which takes care of the content, length, and type.
        body = new HttpEntityBody(entity, contentType);

        Header encoding = entity.getContentEncoding();
        if (encoding != null) {
          builder.header(encoding.getName(), encoding.getValue());
        }
      }
    }
    builder.method(method, body);
    builder.tag(tag);

    return builder.build();
  }
Esempio n. 7
0
  byte[] readBody(HttpResponse httpResponse) throws Exception {
    if (httpResponse == null) return null;
    httpEntity = httpResponse.getEntity();
    if (httpEntity == null) return null;

    Header zipHeader = httpResponse.getFirstHeader("zip");
    boolean zip = zipHeader != null && "true".equals(zipHeader.getValue());

    TimerMonitor timerMonitor = new TimerMonitor(this);
    timerMonitor.startSession();

    contentLengthHeader = httpResponse.getFirstHeader(CONTENT_LENGTH);

    try {
      byte[] bytes = readBody(contentLengthHeader);
      if (zip) bytes = new GZipIO().unzip(bytes);
      if (bytes == null || bytes.length < 1) return bytes;
      readData = bytes.length;
      return decodeResponse(bytes, httpEntity.getContentEncoding());
    } finally {
      if (httpEntity instanceof BasicManagedEntity) {
        BasicManagedEntity entity = (BasicManagedEntity) httpEntity;
        //        System.out.println(" hihi  da thay roi 2 " + entity);
        entity.releaseConnection();
      } else {
        if (httpEntity.getContent() != null) httpEntity.getContent().close();
      }
    }
  }
Esempio n. 8
0
 @Override
 public Header getContentType() {
   if (contentType == null) {
     return httpEntity.getContentEncoding();
   } else {
     return contentType;
   }
 }
  public String getBody(String url) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);

    try {
      HttpResponse response = httpclient.execute(httpGet);
      HttpEntity entity = response.getEntity();

      final String encoding =
          entity.getContentEncoding() != null ? entity.getContentEncoding().getValue() : "UTF-8";

      final String body = (readBody(entity, encoding));

      EntityUtils.consume(entity);
      return body;
    } finally {
      httpGet.releaseConnection();
    }
  }
Esempio n. 10
0
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
  * The encoding is taken from the entity's Content-Encoding header.
  *
  * <p>This is typically used while parsing an HTTP POST.
  *
  * @param entity The entity to parse
  * @throws IOException If there was an exception getting the entity's data.
  */
 public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
   List<NameValuePair> result = Collections.emptyList();
   if (isEncoded(entity)) {
     final String content = EntityUtils.toString(entity);
     final Header encoding = entity.getContentEncoding();
     if (content != null && content.length() > 0) {
       result = new ArrayList<NameValuePair>();
       parse(result, new Scanner(content), encoding != null ? encoding.getValue() : null);
     }
   }
   return result;
 }
  @Test
  public void testCompression() throws Exception {
    try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
      SolrQuery q = new SolrQuery("*:*");

      // verify request header gets set
      DebugServlet.clear();
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNull(DebugServlet.headers.get("Accept-Encoding"));
      client.setAllowCompression(true);
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNotNull(DebugServlet.headers.get("Accept-Encoding"));
      client.setAllowCompression(false);
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNull(DebugServlet.headers.get("Accept-Encoding"));
    }

    // verify server compresses output
    HttpGet get =
        new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml");
    get.setHeader("Accept-Encoding", "gzip");
    CloseableHttpClient httpclient = HttpClientUtil.createClient(null);
    HttpEntity entity = null;
    try {
      HttpResponse response = httpclient.execute(get);
      entity = response.getEntity();
      Header ceheader = entity.getContentEncoding();
      assertEquals("gzip", ceheader.getValue());
    } finally {
      if (entity != null) {
        entity.getContent().close();
      }
      httpclient.close();
    }

    // verify compressed response can be handled
    try (HttpSolrClient client =
        new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) {
      client.setAllowCompression(true);
      SolrQuery q = new SolrQuery("foo");
      QueryResponse response = client.query(q);
      assertEquals(0, response.getStatus());
    }
  }
Esempio n. 12
0
    protected boolean hasEncoding(final HttpResponse response, final String encoding) {
      HttpEntity entity = response.getEntity();
      if (entity == null) return false;
      Header ceHeader = entity.getContentEncoding();
      if (ceHeader == null) return false;

      HeaderElement[] codecs = ceHeader.getElements();
      for (int i = 0; i < codecs.length; i++)
        if (encoding.equalsIgnoreCase(codecs[i].getName())) return true;

      return false;
    }
  private HttpResponse decodeContent(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    if (entity.getContentEncoding() != null) {
      System.out.println("contentEncodingHeaderValue is " + entity.getContentEncoding().getValue());

      HeaderElement[] codecs = entity.getContentEncoding().getElements();
      for (int i = 0; i < codecs.length; i++) {
        String encoding = codecs[i].getName();
        if (encoding.equalsIgnoreCase("gzip")) {
          response.setEntity(new GzipDecompressingEntity(response.getEntity()));
          break;
        } else if (encoding.equals("deflate")) {
          // TODO need implementation
          break;
        } else if (encoding.equals("compress")) {
          // TODO need implementation
          break;
        }
      }
    }

    return response;
  }
Esempio n. 14
0
  public InputStream doPostInputStream(String url, List<NameValuePair> params)
      throws IOException, ReaderException {
    //		Log.d(TAG, "[DEBUG] POST: " + url);
    //		Log.d(TAG, "[DEBUG] PARAMS: " + params);
    //		Log.d(TAG, "[DEBUG] Authorization: " + "GoogleLogin auth=" + this.auth);
    HttpPost post = new HttpPost(url);
    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

    if (this.auth != null) post.setHeader("Authorization", "GoogleLogin auth=" + this.auth);

    // gzip
    post.setHeader("User-agent", "gzip");
    post.setHeader("Accept-Encoding", "gzip");

    HttpResponse res = getClient().execute(post);
    int resStatus = res.getStatusLine().getStatusCode();

    if (resStatus == HttpStatus.SC_FORBIDDEN) throw new ReaderLoginException("Login failure");
    else if (resStatus == HttpStatus.SC_UNAUTHORIZED)
      throw new ReaderLoginException("Authentication fails (" + resStatus + "): " + url);
    else if (resStatus != HttpStatus.SC_OK) {
      throw new ReaderException("Invalid http status " + resStatus + ": " + url);
    }

    final HttpEntity entity = res.getEntity();
    if (entity == null) {
      throw new ReaderException("null response entity");
    }

    InputStream is = null;

    // create the appropriate stream wrapper based on the encoding type
    String encoding = Utils.getHeaderValue(entity.getContentEncoding());
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
      is = new GZIPInputStream(entity.getContent());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
      is = new InflaterInputStream(entity.getContent(), new Inflater(true));
    } else {
      is = entity.getContent();
    }

    return new FilterInputStream(is) {
      @Override
      public void close() throws IOException {
        super.close();
        entity.consumeContent();
      }
    };
  }
Esempio n. 15
0
        public void process(final HttpResponse response, final HttpContext context)
            throws HttpException, IOException {
          HttpEntity entity = response.getEntity();
          Header ceheader = entity.getContentEncoding();
          if (ceheader != null) {
            HeaderElement[] codecs = ceheader.getElements();
            for (HeaderElement codec : codecs) {

              if (codec.getName().equalsIgnoreCase("gzip")) {
                response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                return;
              }
            }
          }
        }
 /**
  * @param response
  * @param context
  * @throws HttpException
  * @throws IOException
  */
 public void process(final HttpResponse response, final HttpContext context)
     throws HttpException, IOException {
   final HttpEntity entity = response.getEntity();
   if (null != entity) {
     final Header encoding = entity.getContentEncoding();
     if (null != encoding) {
       for (HeaderElement codec : encoding.getElements()) {
         if (codec.getName().equalsIgnoreCase("gzip")) {
           response.setEntity(new GzipDecompressingEntity(response.getEntity()));
           return;
         }
       }
     }
   }
 }
 public void process(HttpResponse response, HttpContext context)
     throws HttpException, IOException {
   HttpEntity entity = response.getEntity();
   if (entity != null) {
     Header header = entity.getContentEncoding();
     if (header != null) {
       HeaderElement[] codecs = header.getElements();
       for (int i = 0; i < codecs.length; i++) {
         if (codecs[i].getName().equalsIgnoreCase(GZIP_ENCODING_VALUE)) {
           response.setEntity(new GzipDecompressingEntity(response.getEntity()));
           return;
         }
       }
     }
   }
 }
 public void process(final org.apache.http.HttpResponse response, final HttpContext context)
     throws HttpException, IOException {
   HttpEntity entity = response.getEntity();
   Header ceheader = entity.getContentEncoding();
   if (ceheader != null) {
     HeaderElement[] codecs = ceheader.getElements();
     for (int i = 0; i < codecs.length; i++) {
       if (codecs[i].getName().equalsIgnoreCase("gzip")) {
         response.setEntity(new GzipDecompressingEntity(response.getEntity()));
         return;
       } else if (codecs[i].getName().equalsIgnoreCase("deflate")) {
         response.setEntity(new InflatingEntity(response.getEntity()));
         return;
       }
     }
   }
 }
 private void callMethod(HttpUriRequest request, Operation operation) throws IOException {
   // Specify GData protocol version 2.0.
   request.addHeader("GData-Version", "2");
   // Indicate support for gzip-compressed responses.
   request.addHeader("Accept-Encoding", "gzip");
   // Specify authorization token if provided.
   String authToken = mAuthToken;
   if (!TextUtils.isEmpty(authToken)) {
     request.addHeader("Authorization", "GoogleLogin auth=" + authToken);
   }
   // Specify the ETag of a prior response, if available.
   String etag = operation.inOutEtag;
   if (etag != null) {
     request.addHeader("If-None-Match", etag);
   }
   // Execute the HTTP request.
   HttpResponse httpResponse = null;
   try {
     httpResponse = mHttpClient.execute(request);
   } catch (IOException e) {
     Log.w(TAG, "Request failed: " + request.getURI());
     throw e;
   }
   // Get the status code and response body.
   int status = httpResponse.getStatusLine().getStatusCode();
   InputStream stream = null;
   HttpEntity entity = httpResponse.getEntity();
   if (entity != null) {
     // Wrap the entity input stream in a GZIP decoder if necessary.
     stream = entity.getContent();
     if (stream != null) {
       Header header = entity.getContentEncoding();
       if (header != null) {
         if (header.getValue().contains("gzip")) {
           stream = new GZIPInputStream(stream);
         }
       }
     }
   }
   // Return the stream if successful.
   Header etagHeader = httpResponse.getFirstHeader("ETag");
   operation.outStatus = status;
   operation.inOutEtag = etagHeader != null ? etagHeader.getValue() : null;
   operation.outBody = stream;
 }
Esempio n. 20
0
    /**
     * Send a POST or GET request to the Banner page represented by this BannerForm using either the
     * default value or the value set with one of the set() methods for each parameter passed to the
     * constructor and return the resulting data as a String. After the request completes, all
     * parameters are reset to their default values, and new requests must call the set() methods
     * again to set the values of any parameters they need.
     *
     * <p>This method does not guarantee the format of the results. If any of the constructor
     * arguments were specified incorrectly, if you try to access a page that requires a login to
     * view, or if Banner encounters an internal error (among other things), this method may return
     * an error page, an empty string, or whatever else the server decides to respond to the request
     * with.
     *
     * @return the HTML of the response page, or an empty string if the request fails
     */
    public String request() {
      HttpClient client = new DefaultHttpClient();
      HttpUriRequest request = null;
      BufferedReader reader = null;
      StringBuffer ret = new StringBuffer();

      try {
        if (isPOST) {
          HttpPost post = new HttpPost(BANNER_URL + path);
          List<NameValuePair> parms = new ArrayList<NameValuePair>(defaultParms.length);
          for (Map.Entry<String, String> parm : currentParms.entrySet())
            parms.add(new BasicNameValuePair(parm.getKey(), parm.getValue()));
          post.setEntity(new UrlEncodedFormEntity(parms));
          request = post;
        } else {
          String requestPath = BANNER_URL + path + "?";
          for (Map.Entry<String, String> parm : currentParms.entrySet())
            requestPath += parm.getKey() + "=" + parm.getValue() + "&";
          HttpGet get = new HttpGet(requestPath);
          request = get;
        }
        HttpResponse response = null;
        response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        Header encoding = entity.getContentEncoding();
        reader =
            new BufferedReader(
                new InputStreamReader(
                    stream, (encoding == null ? "iso-8859-1" : encoding.getValue())));
        for (String line = reader.readLine(); line != null; line = reader.readLine())
          ret.append(line);
        reader.close();
      } catch (UnsupportedEncodingException uee) {
        Log.e(TAG, "Failed to send Banner request", uee);
      } catch (ClientProtocolException cpe) {
        Log.e(TAG, "Failed to send Banner request", cpe);
      } catch (IOException ioe) {
        Log.e(TAG, "Failed to send Banner request", ioe);
      } finally {
        for (NameValuePair parm : defaultParms) currentParms.put(parm.getName(), parm.getValue());
      }

      return ret.toString();
    }
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
  * The encoding is taken from the entity's Content-Encoding header.
  *
  * <p>This is typically used while parsing an HTTP POST.
  *
  * @param entity The entity to parse
  * @throws IOException If there was an exception getting the entity's data.
  */
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2013-12-30 13:01:44.455 -0500",
     hash_original_method = "3125ACC43E08755650BD72975049DA9D",
     hash_generated_method = "57408A403AC39C54BE0F6AE05B140555")
 public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
   List<NameValuePair> result = Collections.emptyList();
   if (isEncoded(entity)) {
     final String content = EntityUtils.toString(entity);
     final Header encoding = entity.getContentEncoding();
     if (content != null && content.length() > 0) {
       result = new ArrayList<NameValuePair>();
       parse(result, new Scanner(content), encoding != null ? encoding.getValue() : null);
     }
   }
   return result;
 }
 @Test
 public void testPrepareInputLengthDelimited() throws Exception {
   final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
   message.addHeader("Content-Length", "10");
   message.addHeader("Content-Type", "stuff");
   message.addHeader("Content-Encoding", "identity");
   final HttpEntity entity = conn.prepareInput(message);
   Assert.assertNotNull(entity);
   Assert.assertFalse(entity.isChunked());
   Assert.assertEquals(10, entity.getContentLength());
   final Header ct = entity.getContentType();
   Assert.assertNotNull(ct);
   Assert.assertEquals("stuff", ct.getValue());
   final Header ce = entity.getContentEncoding();
   Assert.assertNotNull(ce);
   Assert.assertEquals("identity", ce.getValue());
   final InputStream instream = entity.getContent();
   Assert.assertNotNull(instream);
   Assert.assertTrue((instream instanceof ContentLengthInputStream));
 }
  @Test
  public void testTooLargeEntityHasOriginalContentTypes() throws Exception {
    HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    StringEntity entity = new StringEntity("large entity content");
    response.setEntity(entity);

    impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);

    impl.readResponse();
    boolean tooLarge = impl.isLimitReached();
    HttpResponse result = impl.getReconstructedResponse();
    HttpEntity reconstructedEntity = result.getEntity();
    Assert.assertEquals(entity.getContentEncoding(), reconstructedEntity.getContentEncoding());
    Assert.assertEquals(entity.getContentType(), reconstructedEntity.getContentType());

    String content = EntityUtils.toString(reconstructedEntity);

    Assert.assertTrue(tooLarge);
    Assert.assertEquals("large entity content", content);
  }
Esempio n. 24
0
  /** Loads the content of this page from a fetched HttpEntity. */
  public void load(HttpEntity entity) throws Exception {

    contentType = null;
    Header type = entity.getContentType();
    if (type != null) {
      contentType = type.getValue();
    }

    contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) {
      contentEncoding = encoding.getValue();
    }

    Charset charset = ContentType.getOrDefault(entity).getCharset();
    if (charset != null) {
      contentCharset = charset.displayName();
    }

    contentData = EntityUtils.toByteArray(entity);
  }
Esempio n. 25
0
  public static Map<String, Object> doPostToWx(String url, String outstr) {

    // 创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(url);

    Map<String, Object> rtnMap = null;
    try {
      httpPost.setEntity(new StringEntity(outstr, "UTF-8"));
      HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
      // 获取响应消息实体
      HttpEntity entity = httpResponse.getEntity();
      // 响应状态
      logger.debug("status:{}", httpResponse.getStatusLine());
      // 判断响应实体是否为空
      if (entity != null) {
        logger.debug("contentEncoding:" + entity.getContentEncoding());
        String responseStr = EntityUtils.toString(entity, "UTF-8");
        logger.debug("response content\n", responseStr);

        rtnMap = JsonUtil.fromJson(responseStr, Map.class);

        logger.debug("返回的Map:\n{}", rtnMap);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 关闭流并释放资源
        closeableHttpClient.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return rtnMap;
  }
Esempio n. 26
0
  @Test
  public void test03() {
    HttpClient httpClient = buildHttpClient();
    HttpGet httpGet = new HttpGet(HTTPGETTESTURL);
    logger.info("httpGet.getRequestLine():{}", httpGet.getRequestLine());

    try {
      HttpResponse httpResponse = httpClient.execute(httpGet); // 执行get请求
      HttpEntity httpEntity = httpResponse.getEntity(); // 获取响应消息实体
      logger.info("响应状态:{}", httpResponse.getStatusLine());
      if (httpEntity != null) {
        logger.info("contentEncoding:{}", httpEntity.getContentEncoding());
        logger.info("response content:{}", EntityUtils.toString(httpEntity));
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

    }
  }
Esempio n. 27
0
  private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
      String urlstr = coreUrl + queryParams.toQueryString();

      boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
      if ("GET".equals(req.getMethod())) {
        method = new HttpGet(urlstr);
      } else if ("HEAD".equals(req.getMethod())) {
        method = new HttpHead(urlstr);
      } else if (isPostOrPutRequest) {
        HttpEntityEnclosingRequestBase entityRequest =
            "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
        InputStream in =
            new CloseShieldInputStream(req.getInputStream()); // Prevent close of container streams
        HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
        entityRequest.setEntity(entity);
        method = entityRequest;
      } else if ("DELETE".equals(req.getMethod())) {
        method = new HttpDelete(urlstr);
      } else {
        throw new SolrException(
            SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
      }

      for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
        String headerName = e.nextElement();
        if (!"host".equalsIgnoreCase(headerName)
            && !"authorization".equalsIgnoreCase(headerName)
            && !"accept".equalsIgnoreCase(headerName)) {
          method.addHeader(headerName, req.getHeader(headerName));
        }
      }
      // These headers not supported for HttpEntityEnclosingRequests
      if (method instanceof HttpEntityEnclosingRequest) {
        method.removeHeaders(TRANSFER_ENCODING_HEADER);
        method.removeHeaders(CONTENT_LENGTH_HEADER);
      }

      final HttpResponse response =
          solrDispatchFilter.httpClient.execute(
              method, HttpClientUtil.createNewHttpClientRequestContext());
      int httpStatus = response.getStatusLine().getStatusCode();
      httpEntity = response.getEntity();

      resp.setStatus(httpStatus);
      for (HeaderIterator responseHeaders = response.headerIterator();
          responseHeaders.hasNext(); ) {
        Header header = responseHeaders.nextHeader();

        // We pull out these two headers below because they can cause chunked
        // encoding issues with Tomcat
        if (header != null
            && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER)
            && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
          resp.addHeader(header.getName(), header.getValue());
        }
      }

      if (httpEntity != null) {
        if (httpEntity.getContentEncoding() != null)
          resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
        if (httpEntity.getContentType() != null)
          resp.setContentType(httpEntity.getContentType().getValue());

        InputStream is = httpEntity.getContent();
        OutputStream os = resp.getOutputStream();

        IOUtils.copyLarge(is, os);
      }

    } catch (IOException e) {
      sendError(
          new SolrException(
              SolrException.ErrorCode.SERVER_ERROR,
              "Error trying to proxy request for url: " + coreUrl,
              e));
    } finally {
      Utils.consumeFully(httpEntity);
    }
  }
Esempio n. 28
0
  @Override
  protected void doExecute(Parameters config, Parameters output) {
    CloseableHttpClient httpClient = DetectiveFactory.INSTANCE.getHttpClient();

    CookieStore cookieStore = null;
    boolean useSharedCookies =
        this.readAsString(config, "http.use_shared_cookies", "true", true, null).equals("true");
    if (useSharedCookies) {
      cookieStore = this.readOptional(config, PARAM_HTTP_COOKIES, null, CookieStore.class);
    }
    if (cookieStore == null) {
      cookieStore = new BasicCookieStore();
    }
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(cookieStore);
    context.setRequestConfig(
        RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build());

    String method = this.readAsString(config, "http.method", "post", true, null);
    HttpMethod m = HttpMethod.valueOf(method.toUpperCase());
    Object address =
        this.readAsObject(
            config,
            "http.address",
            null,
            false,
            "please provider address your request send to",
            Object.class);
    URI uri = null;
    if (address instanceof String) uri = URI.create((String) address);
    else if (address instanceof GString) uri = URI.create(address.toString());
    else if (address instanceof URI) uri = (URI) address;
    else
      throw new ConfigException(
          "the address you provided have to be a String or java URI, however your type is "
              + address.getClass().getName());

    HttpUriRequest request = this.createHttpUriRequest(m, uri);
    request.setHeader(
        "User-Agent",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36");

    String userName =
        this.readAsString(
            config, "http.auth.username", null, true, "Adding basic http header auth to request");
    String password = this.readAsString(config, "http.auth.password", null, true, "");
    if (null != userName && null != password) {
      request = addBasicAuthentication(request, userName, password);
    }

    String authorizationHeader =
        this.readAsString(
            config,
            "http.auth.header",
            null,
            true,
            "No authorization header found, skipping token auth");
    if (authorizationHeader != null) {
      request = addAuthorizationHeader(request, authorizationHeader);
    }

    if (request instanceof HttpEntityEnclosingRequestBase) {
      if (config.containsKey("http.post.string")) {
        Object postText = this.readOptional(config, "http.post.string", null, Object.class);
        setupPostRequest((HttpEntityEnclosingRequestBase) request, postText.toString());
      } else if (config.containsKey("http.post.file.filename")) {
        String filename = this.readOptional(config, "http.post.file.filename", null, String.class);
        this.setupPostWithFileName((HttpPost) request, filename);
      }
    }

    try {
      CloseableHttpResponse response = httpClient.execute(request, context);
      try {
        output.put(PARAM_HTTP_COOKIES, cookieStore);
        output.put(
            "http.protocal.name", response.getStatusLine().getProtocolVersion().getProtocol());
        output.put(
            "http.protocal.version.major",
            response.getStatusLine().getProtocolVersion().getMajor());
        output.put(
            "http.protocal.version.minor",
            response.getStatusLine().getProtocolVersion().getMinor());
        output.put(
            "http.protocal.description", response.getStatusLine().getProtocolVersion().toString());
        output.put("http.status.code", response.getStatusLine().getStatusCode());
        output.put("http.status.reason", response.getStatusLine().getReasonPhrase());

        // Headers
        for (Header header : response.getAllHeaders()) {
          output.put("http.header." + header.getName(), header.getValue());
        }

        // Cookies
        // output.put("http.cookies", context.getCookieStore().getCookies());

        // Entity
        HttpEntity entity = response.getEntity();
        // add by George Zeng, for adding a content back to do more actions
        Scanner scanner = new Scanner(entity.getContent());
        StringBuilder content = new StringBuilder();
        while (scanner.hasNext()) {
          content.append(scanner.nextLine()).append("\n");
        }
        scanner.close();
        output.put("http.content", content.toString());
        output.put("http.content.string", content.toString());
        output.put("http.content.length", entity.getContentLength());
        if (entity.getContentEncoding() != null)
          output.put(
              "http.content." + entity.getContentEncoding().getName(),
              entity.getContentEncoding().getValue());
        if (entity.getContentType() != null)
          output.put(
              "http.content." + entity.getContentType().getName(),
              entity.getContentType().getValue());

        // System.out.println(entity.toString());
      } finally {
        response.close();
      }
    } catch (ClientProtocolException ex) {
      throw new TaskException(ex);
    } catch (IOException ex) {
      throw new TaskException(ex);
    }
  }
    public void run() {
      try {
        HttpUriRequest request = mConn.getHttpRequest();
        InetAddress ia = InetAddress.getByName(request.getURI().getHost());
        LogUtils.logd("Requesting: " + mConn.getRequestUrl() + " (" + ia.getHostAddress() + ")");

        request.addHeader("User-Agent", USER_AGENT);
        for (NameValuePair header : mConn.getRequestHeaders()) {
          request.addHeader(header.getName(), header.getValue());
        }

        LogUtils.logd(
            "Headers: " + Arrays.asList(mConn.getHttpRequest().getAllHeaders()).toString());
        if (mConn.getHttpRequest() instanceof HttpPost) {
          String postBody = EntityUtils.toString(((HttpPost) mConn.getHttpRequest()).getEntity());
          LogUtils.logd("POST to " + mConn.getRequestUrl() + ": " + postBody);
        }

        HttpResponse response;
        try {
          response = mHttpClient.execute(request);
        } catch (IOException e) {
          // XXX Mediocre way to match exceptions from aborted requests:
          if (request.isAborted() && e.getMessage().contains("abort")) {
            throw new AbortedRequestException();
          } else {
            throw e;
          }
        }

        if (request.isAborted()) throw new AbortedRequestException();

        // Fetching the status code allows the response interceptor to have a chance to un-gzip the
        // entity before we fetch it.
        response.getStatusLine().getStatusCode();

        HttpResponseHeaders headers = HttpResponseHeaders.fromResponse(response, request);

        HttpEntity entity = response.getEntity();
        byte[] responseBody;
        if (entity == null) {
          responseBody = new byte[0];
        } else {
          responseBody = EntityUtils.toByteArray(entity);
          entity.consumeContent();

          final Header encoding = entity.getContentEncoding();
          if (encoding != null) {
            for (HeaderElement element : encoding.getElements()) {
              if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(responseBody));
                responseBody = IoUtils.readAndClose(gis, true);
                break;
              }
            }
          }
        }

        AsyncHttpResponse ahr;
        String statusLine = response.getStatusLine().toString();
        String bodyStr = new String(responseBody);
        int bodySubStrLen = bodyStr.length() > 300 ? 300 : bodyStr.length();

        switch (response.getStatusLine().getStatusCode()) {
          case HttpStatus.SC_OK:
            // Normal success
          case HttpStatus.SC_NOT_MODIFIED:
            // From mobile_config_and_baseurl called with an Etag
          case HttpStatus.SC_MOVED_PERMANENTLY:
          case HttpStatus.SC_SEE_OTHER:
          case HttpStatus.SC_TEMPORARY_REDIRECT:
          case HttpStatus.SC_MOVED_TEMPORARILY:
            // for UPS-1390 - don't error on 302s from token URL
          case HttpStatus.SC_CREATED:
            // Response from the Engage trail creation and maybe URL shortening calls
            LogUtils.logd(statusLine + ": " + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, null, headers, responseBody);
            break;
          default:
            LogUtils.loge(statusLine + "\n" + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, new Exception(statusLine), headers, responseBody);
        }

        mConn.setResponse(ahr);
        invokeCallback(callBack);
      } catch (IOException e) {
        LogUtils.loge(this.toString());
        LogUtils.loge("IOException while executing HTTP request.", e);
        mConn.setResponse(new AsyncHttpResponse(mConn, e, null, null));
        invokeCallback(callBack);
      } catch (AbortedRequestException e) {
        LogUtils.loge("Aborted request: " + mConn.getRequestUrl());
        mConn.setResponse(new AsyncHttpResponse(mConn, null, null, null));
        invokeCallback(callBack);
      }
    }
 @Override
 public Header getContentEncoding() {
   return delegate.getContentEncoding();
 }