Beispiel #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());
      }
    }
  }
Beispiel #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);
      }
    }
  }
    @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();
      }
    }
Beispiel #4
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;
  }