Ejemplo n.º 1
0
  /**
   * 响应中获取cookie
   *
   * @param response
   */
  public void getResponseCookies(HttpResponse response) {
    // 法1
    HeaderIterator headerIter = response.headerIterator("Set-Cookie");
    while (headerIter.hasNext()) {
      Object obj = headerIter.next();
      if (obj != null) {
        String cookie = ((Header) headerIter.next()).getValue();

        String cookieRecord = cookie.substring(0, cookie.indexOf(';'));
        System.out.println(cookieRecord);
      }
    }

    // 法2
    HeaderElementIterator headerElementIter =
        new BasicHeaderElementIterator(response.headerIterator("Set-Cookie"));
    while (headerElementIter.hasNext()) {
      HeaderElement elem = headerElementIter.nextElement();
      System.out.println("Element : " + elem.getName() + " = " + elem.getValue());
      NameValuePair[] params = elem.getParameters();
      for (int i = 0; i < params.length; i++) {
        System.out.println(" Params : " + params[i].getName() + " | " + params[i].getValue());
      }
    }
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws ClientProtocolException, IOException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

      // HttpClient httpClient = getTestHttpClient();
      // HttpHost proxy = new HttpHost("127.0.0.1", 8888);
      // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
      // proxy);

      String apiKey = args[0];
      String latitude = "46.947922";
      String longitude = "7.444608";

      String urlTemplate = "https://api.forecast.io/forecast/%s/%s,%s";
      String url = String.format(urlTemplate, apiKey, latitude, longitude);
      System.out.println(url);

      HttpGet httpget = new HttpGet(url);
      try (CloseableHttpResponse response = httpClient.execute(httpget)) {
        System.out.println(response.getFirstHeader("Content-Encoding"));
        HeaderIterator it = response.headerIterator();
        while (it.hasNext()) {
          Object header = it.next();
          System.out.println(header);
        }

        HttpEntity entity = response.getEntity();
        String jsonData = EntityUtils.toString(entity, StandardCharsets.UTF_8);

        System.out.println(jsonData);
      }
    }
  }
Ejemplo n.º 3
0
  private static Map<String, String> mapHeaders(HeaderIterator it) {
    Map<String, String> map = new HashMap<String, String>();

    while (it.hasNext()) {
      Header h = it.nextHeader();
      map.put(h.getName(), h.getValue());
    }

    return map;
  }
Ejemplo n.º 4
0
 public Set<String> getAllowedMethods(HttpResponse response) {
   if (response == null) {
     throw new IllegalArgumentException("HTTP response may not be null");
   }
   HeaderIterator it = response.headerIterator(AllowHeader.NAME);
   Set<String> methods = new HashSet();
   while (it.hasNext()) {
     for (HeaderElement element : it.nextHeader().getElements()) {
       methods.add(element.getName());
     }
   }
   return methods;
 }
    @Override
    public void run() {
      try {
        HttpResponse response = this.httpClient.execute(this.httpget, this.context);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
          // do something
          logger.warn("url:" + this.httpget.getURI());
          logger.warn(response.getStatusLine().getStatusCode() + ":");
          for (HeaderIterator itor = response.headerIterator(); itor.hasNext(); ) {
            logger.warn("  " + itor.next());
          }
          logger.warn("内容:" + EntityUtils.toString(entity));

          // 保证连接能释放会管理器
          entity.consumeContent();
        }
      } catch (Exception ex) {
        this.httpget.abort();
      }
    }
Ejemplo n.º 6
0
  @Override
  public ProtocolResponse handleResponse(HttpResponse response)
      throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    Metadata metadata = new Metadata();
    HeaderIterator iter = response.headerIterator();
    while (iter.hasNext()) {
      Header header = iter.nextHeader();
      metadata.addValue(header.getName().toLowerCase(Locale.ROOT), header.getValue());
    }

    MutableBoolean trimmed = new MutableBoolean();

    byte[] bytes = HttpProtocol.toByteArray(response.getEntity(), maxContent, trimmed);

    if (trimmed.booleanValue()) {
      metadata.setValue("http.trimmed", "true");
      LOG.warn("HTTP content trimmed to {}", bytes.length);
    }

    return new ProtocolResponse(bytes, status, metadata);
  }
    /**
     * Implements a patch out in 4.1.x and 4.2 that isn't available in 4.0.x which fixes a bug where
     * connections aren't reused when the response is gzipped. See
     * https://issues.apache.org/jira/browse/HTTPCORE-257 for info about the issue, and
     * http://svn.apache.org/viewvc?view=revision&revision=1124215 for the patch which is copied
     * here.
     */
    @Override
    public boolean keepAlive(final HttpResponse response, final HttpContext context) {
      if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null.");
      }
      if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null.");
      }

      // Check for a self-terminating entity. If the end of the entity
      // will
      // be indicated by closing the connection, there is no keep-alive.
      ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
      Header teh = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
      if (teh != null) {
        if (!HTTP.CHUNK_CODING.equalsIgnoreCase(teh.getValue())) {
          return false;
        }
      } else {
        Header[] clhs = response.getHeaders(HTTP.CONTENT_LEN);
        // Do not reuse if not properly content-length delimited
        if (clhs == null || clhs.length != 1) {
          return false;
        }
        Header clh = clhs[0];
        try {
          int contentLen = Integer.parseInt(clh.getValue());
          if (contentLen < 0) {
            return false;
          }
        } catch (NumberFormatException ex) {
          return false;
        }
      }

      // Check for the "Connection" header. If that is absent, check for
      // the "Proxy-Connection" header. The latter is an unspecified and
      // broken but unfortunately common extension of HTTP.
      HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
      if (!hit.hasNext()) hit = response.headerIterator("Proxy-Connection");

      // Experimental usage of the "Connection" header in HTTP/1.0 is
      // documented in RFC 2068, section 19.7.1. A token "keep-alive" is
      // used to indicate that the connection should be persistent.
      // Note that the final specification of HTTP/1.1 in RFC 2616 does
      // not
      // include this information. Neither is the "Connection" header
      // mentioned in RFC 1945, which informally describes HTTP/1.0.
      //
      // RFC 2616 specifies "close" as the only connection token with a
      // specific meaning: it disables persistent connections.
      //
      // The "Proxy-Connection" header is not formally specified anywhere,
      // but is commonly used to carry one token, "close" or "keep-alive".
      // The "Connection" header, on the other hand, is defined as a
      // sequence of tokens, where each token is a header name, and the
      // token "close" has the above-mentioned additional meaning.
      //
      // To get through this mess, we treat the "Proxy-Connection" header
      // in exactly the same way as the "Connection" header, but only if
      // the latter is missing. We scan the sequence of tokens for both
      // "close" and "keep-alive". As "close" is specified by RFC 2068,
      // it takes precedence and indicates a non-persistent connection.
      // If there is no "close" but a "keep-alive", we take the hint.

      if (hit.hasNext()) {
        try {
          TokenIterator ti = createTokenIterator(hit);
          boolean keepalive = false;
          while (ti.hasNext()) {
            final String token = ti.nextToken();
            if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
              return false;
            } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
              // continue the loop, there may be a "close"
              // afterwards
              keepalive = true;
            }
          }
          if (keepalive) return true;
          // neither "close" nor "keep-alive", use default policy

        } catch (ParseException px) {
          // invalid connection header means no persistent connection
          // we don't have logging in HttpCore, so the exception is
          // lost
          return false;
        }
      }

      // default since HTTP/1.1 is persistent, before it was
      // non-persistent
      return !ver.lessEquals(HttpVersion.HTTP_1_0);
    }
Ejemplo n.º 8
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);
    }
  }
Ejemplo n.º 9
0
  public static int querySid(
      String check, String ssid, String ccpId, String ccpsession, int version) {
    HttpPost queryPost =
        new HttpPost(
            "https://webim.feixin.10086.cn/content/WebIM/AddBuddy.aspx?Version=" + version++);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("AddType", "1"));
    nvps.add(new BasicNameValuePair("BuddyLists", "0"));
    nvps.add(new BasicNameValuePair("Ccp", check));
    nvps.add(new BasicNameValuePair("CcpId", ccpId));
    nvps.add(new BasicNameValuePair("Desc", "马勇"));
    nvps.add(new BasicNameValuePair("LocalName", null));
    nvps.add(new BasicNameValuePair("PhraseId", "0"));
    nvps.add(new BasicNameValuePair("ssid", ssid));
    nvps.add(new BasicNameValuePair("SubscribeFlag", "0"));
    nvps.add(new BasicNameValuePair("UserName", "13636532333"));

    try {
      queryPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    } catch (UnsupportedEncodingException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    queryPost.addHeader(
        "Cookie",
        "IsCookiesEnable=true; webim_loginCounter=1342244257406; ccpsession="
            + ccpsession
            + "; webim_usersid=645048052; webim_userstatus=400; webim_login_status=0; webim_remindmsgs=645048052-39532%2521191-0-a56dcfc5f2c943adb168c91f6456fe59");
    queryPost.addHeader("Accept", "application/json, text/javascript, */*");
    queryPost.addHeader("Accept-Encoding", "gzip, deflate");
    queryPost.addHeader("Accept-Language", "zh-cn");
    queryPost.addHeader("x-requested-with", "XMLHttpRequest");
    queryPost.addHeader("Referer", "https://webim.feixin.10086.cn/content/addBuddy.htm?tabIndex=1");
    queryPost.addHeader(
        "User-Agent",
        "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");

    try {
      HttpResponse smResponse = getHttpclient().execute(queryPost);
      log.info(smResponse.getEntity().getContent().toString());
      HeaderIterator postHi = smResponse.headerIterator("Set-Cookie");
      while (postHi.hasNext()) {
        String setCookie = postHi.next().toString();
        // get the cookie from the string
        setCookie = setCookie.substring(setCookie.indexOf(":") + 1);
        setCookie = setCookie.substring(1, setCookie.indexOf(";"));
        log.info(setCookie);
      }
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      queryPost.abort();
    }
    return version;
  }