当前位置 : 主页 > 编程语言 > c++ >

httpclient 4.5 使用demon 以及 模拟登陆demon

来源:互联网 收集:自由互联 发布时间:2021-07-03
httpclient 4.5 使用demon import org.apache.http.*;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.h
httpclient 4.5 使用demon
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zcx on 2017/8/17.
 */
public class HttpClientutil {





    public final static void main(String[] args) throws Exception {

        String jj=posthtml("http://www.dreamtowns.cn/taskDemand/jts_findTaskDemandList.jhtml",null);
        System.out.println(jj);
    }





    public static String gethtml(String url,BasicHeader ...headers){

        RequestConfig requestConfig=RequestConfig.custom().setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000).build();

        CloseableHttpClient httpClient= HttpClients.createDefault();

        HttpGet httpget=new HttpGet(url);

        httpget.setConfig(requestConfig);

        httpget.setHeader(HttpHeaders.USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");

        httpget.setHeader(HttpHeaders.ACCEPT,"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");

        httpget.setHeader(HttpHeaders.ACCEPT_ENCODING,"gzip, deflate");

        httpget.setHeader(HttpHeaders.ACCEPT_LANGUAGE,"zh-CN,zh;q=0.8");http://www.dreamtowns.cn/

        httpget.setHeader(HttpHeaders.CONNECTION,"keep-alive");

        if(headers!=null&&headers.length>0){
            httpget.setHeaders(headers);
        }

        CloseableHttpResponse response=null;
        try {
             response=httpClient.execute(httpget);
            HttpEntity entity=response.getEntity();
            return EntityUtils.toString(entity,"utf-8");

          } catch (IOException e) {
            e.printStackTrace();
          }finally {
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }


  public static String posthtml(String url, String content, BasicHeader... headers){


        CloseableHttpClient httpclient=HttpClients.createDefault();

        RequestConfig config=RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setRedirectsEnabled(true).build();

        HttpPost  post=new HttpPost(url);

        post.setHeader(HttpHeaders.USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");

        post.setHeader(HttpHeaders.ACCEPT,"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");

        post.setHeader(HttpHeaders.ACCEPT_ENCODING,"gzip, deflate");

        post.setHeader(HttpHeaders.ACCEPT_LANGUAGE,"zh-CN,zh;q=0.8");

        post.setHeader(HttpHeaders.CONNECTION,"keep-alive");

        //post.setHeader("Cookie","acw_tc=AQAAAMOObXBCFgQA9oBBcFVo0hUYtY5r; JSESSIONID=90E3C5F04E1626F33B806D265876C4A2; Hm_lvt_d00d7d1ced351c0cb4b00918f6f07e2d=1502693363,1502788805,1502958403,1503020791; Hm_lpvt_d00d7d1ced351c0cb4b00918f6f07e2d=1503020804; clientlanguage=zh_CN");

       // post.setHeader(HttpHeaders.REFERER,"http://www.dreamtowns.cn/");

        post.setConfig(config);

//      StringEntity entity = new StringEntity(content, Charset.forName("UTF-8"));
//      entity.setContentEncoding("UTF-8");
      List
 
   params = new ArrayList
  
   (); if(content!=null){ String [] res=content.split(";"); for (String ccs:res) { String skt=ccs.split("=")[0]; String skt1=ccs.split("=")[1]; params.add(new BasicNameValuePair(skt1,skt)); } } CloseableHttpResponse response=null; try { if(!params.isEmpty()){ post.setEntity(new UrlEncodedFormEntity(params)); } response=httpclient.execute(post); int yy=response.getStatusLine().getStatusCode(); if (yy==302){ gethtml("http://www.dreamtowns.cn/"); } System.out.println(yy); return EntityUtils.toString(response.getEntity(),"utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } } ++++++++++++++++++++模拟登陆++++++++++++++++++++ import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.util.EntityUtils; import java.net.URI; import java.net.URL; import java.util.List; /** * Created by zcx on 2017/8/17. */ public class login { public static void main(String[] args) throws Exception { BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCookieStore(cookieStore) .build(); try { HttpGet httpget = new HttpGet("http://www.dreamtowns.cn/"); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List
   
     cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://www.dreamtowns.cn/login.jspx")) .addParameter("username", "uid") .addParameter("password", "pw") .addParameter("returnUrl","\\") .build(); CloseableHttpResponse response2 = httpclient.execute(login); // String kk =response2.getHeaders("Location")[0].toString(); // if(response2.getStatusLine().getStatusCode()==302){ // HttpUriRequest httpget1 = RequestBuilder.get().setUri(new URI("http://www.dreamtowns.cn/")).build(); // CloseableHttpResponse response3=httpclient.execute(httpget1); // System.out.println(EntityUtils.toString(response3.getEntity())); // } // System.out.println(kk); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form get: " + response2.getStatusLine()); EntityUtils.consume(entity); System.out.println("Post logon cookies:"); List
    
      cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } } } ++++++++++++++++++pom.xml++++++++++++++ 
      
      
      
       org.apache.httpcomponents
       
      
       httpclient
       
      
       4.5.3
       
     
    
   
  
 
网友评论