比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

HttpClient4.X的代理添加实现

HttpClient4.X的代理添加实现http://www.bieryun.com/1630.html

[java] 查看纯文本

  1. package  org.yla.test;
  2. import  java.net.URI;
  3. import  java.util.ArrayList;
  4. import  java.util.List;
  5. import  org.apache.commons.httpclient.HttpClient;
  6. import  org.apache.commons.httpclient.methods.GetMethod;
  7. import  org.apache.http.Header;
  8. 导入 org.apache.http.HttpEntity;
  9. import  org.apache.http.HttpHost;
  10. 导入 org.apache.http.auth.AuthScope;
  11. import  org.apache.http.auth.Credentials;
  12. import  org.apache.http.auth.UsernamePasswordCredentials;
  13. 导入 org.apache.http.client.CredentialsProvider;
  14. import  org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import  org.apache.http.client.methods.CloseableHttpResponse;
  16. import  org.apache.http.client.methods.HttpGet;
  17. import  org.apache.http.client.methods.HttpPost;
  18. import  org.apache.http.impl.client.BasicCredentialsProvider;
  19. import  org.apache.http.impl.client.CloseableHttpClient;
  20. import  org.apache.http.impl.client.HttpClientBuilder;
  21. import  org.apache.http.impl.client.HttpClients;
  22. import  org.apache.http.message.BasicNameValuePair;
  23. import  org.apache.http.util.EntityUtils;
  24. import  org.junit.Test;
  25. 公共 HttpClientTest {
  26.     String url =  “xxxxxxxxxxxxxxxxxxxxxxxxx” ;
  27.     String ip =  “202.107.233.85” ;
  28.     int  port =  8080 ;
  29.     字符串用户名=  “” ;
  30.     String password =  “” ;
  31.     / **
  32.      *使用HttpClient4实现代理202.107.233.85 8080
  33.      * 
  34.      * @异常
  35.      * /
  36.     @测试
  37.     公共无效 test1()  抛出 异常{
  38.         HttpClientBuilder build = HttpClients.custom();
  39.         HttpHost proxy =  new  HttpHost(ip,port);
  40.         CloseableHttpClient client = build.setProxy(proxy).build();
  41.         HttpGet request =  new  HttpGet(url);
  42.         CloseableHttpResponse response = client.execute(request);
  43.         HttpEntity entity = response.getEntity();
  44.         的System.out.println(EntityUtils.toString(实体));
  45.     }
  46.     / **
  47.      *使用httpclient3实现代理
  48.      * 
  49.      * @异常
  50.      * /
  51.     @测试
  52.     公共无效 test2()  抛出 异常{
  53.         HttpClient httpClient =  new  HttpClient();
  54.         httpClient.getHostConfiguration()。setProxy(ip,port);
  55.         GetMethod方法=  new  GetMethod(url);
  56.         httpClient.executeMethod(方法);
  57.         String result =  new  String(method.getResponseBody());
  58.         的System.out.println(结果);
  59.     }
  60.     / **
  61.      *使用httpclient4实现代理(带密码的代理)
  62.      * 
  63.      * @异常
  64.      * /
  65.     @测试
  66.     公共无效 test3()  抛出 异常{
  67.         HttpClientBuilder build = HttpClients.custom();
  68.         CredentialsProvider credentialsProvider =  new  BasicCredentialsProvider();
  69.         AuthScope authscope =  new  AuthScope(ip,port);
  70.         凭证凭证=  新的 UsernamePasswordCredentials(用户名,
  71.                 密码);
  72.         credentialsProvider.setCredentials(authscope,credentials);
  73.         CloseableHttpClient client = build.setDefaultCredentialsProvider(
  74.                 credentialsProvider).build();
  75.         HttpGet request =  new  HttpGet(url);
  76.         CloseableHttpResponse response = client.execute(request);
  77.         HttpEntity entity = response.getEntity();
  78.         的System.out.println(EntityUtils.toString(实体));
  79.     }
  80.     / **
  81.      *使用httpclient3实现代理(带密码的代理)
  82.      * 
  83.      * @异常
  84.      * /
  85.     @测试
  86.     公共无效 test4()  抛出 异常{
  87.         HttpClient httpClient =  new  HttpClient();
  88.         org.apache.commons.httpclient.auth.AuthScope authscope =  new  org.apache.commons.httpclient.auth.AuthScope(
  89.                 ip,port);
  90.         org.apache.commons.httpclient.Credentials credentials =  new org.apache.commons.httpclient.UsernamePasswordCredentials(
  91.                 用户名密码);
  92.         httpClient.getState()。setProxyCredentials(authscope,credentials);
  93.         GetMethod方法=  new  GetMethod(url);
  94.         httpClient.executeMethod(方法);
  95.         String result =  new  String(method.getResponseBody());
  96.         的System.out.println(结果);
  97.     }
  98.     / **
  99.      *模拟登录官网(http://mis.pyc.com.cn?
  100.      * 
  101.      * @异常
  102.      * /
  103.     @测试
  104.     公共无效 testLogin()  抛出 异常{
  105.         HttpClientBuilder build = HttpClients.custom();
  106.         CloseableHttpClient client = build.build();
  107.         HttpPost post =  new  HttpPost(“http://mis.pyc.com.cn/login.aspx” );
  108.         List <BasicNameValuePair> params =  new  ArrayList <BasicNameValuePair>();
  109.         params.add(new  BasicNameValuePair(__VIEWSTATE
  110.                 “/ wEPDwUJNjUwNzE0MTM4ZGQzh + vF2xGjdG8Q15kIqgR0CpxhmPucdCqZOPcglRZr / w ==” ));
  111.         params.add(new  BasicNameValuePair(
  112.                 “__EVENTVALIDATION”
  113.                 “/ wEWBQLYtKSdCALEhISFCwKd + 7qdDgKC3IeGDAK7q7GGCOqhJpRD8S8yy3ZAlPTSsmPzRUoXMK0mQvGgzlk6hm + G” ));
  114.         params.add(new  BasicNameValuePair(“txtName” ,  “xxxxx” ));
  115.         params.add(new  BasicNameValuePair(“txtPwd” ,  “xxxxxx” ));
  116.         params.add(new  BasicNameValuePair(“btnLogin” ,  “xxxx” ));
  117.         HttpEntity entity =  new  UrlEncodedFormEntity(params,  “UTF-8” );
  118.         post.setEntity(实体);
  119.         CloseableHttpResponse response = client.execute(post);
  120.         int  statusCode = response.getStatusLine()。getStatusCode();
  121.         System.err.println(“状态”  + statusCode);
  122.         if  (statusCode ==  302 ){
  123.             Header [] location = response.getHeaders(“location” );
  124.             String rediretUrl =  null ;
  125.             if  (location.length ==  1 ){
  126.                 rediretUrl =  “http://mis.pyc.com.cn”  + location [ 0 ] .getValue();
  127.                 System.err.println(“跳转地址:”  + rediretUrl);
  128.             }
  129.             Header [] allHeaders = response.getAllHeaders();
  130.             System.out.println(“================== response ===================” );
  131.             for  (Header header:allHeaders){
  132.                 System.err.println(header.getName()+  “:”  + header.getValue());
  133.             }
  134.             标头cookieHeader = response.getFirstHeader(“Set-Cookie” );
  135.             String cookie = cookieHeader.getValue();
  136.             System.out.println(“cookie:”  + cookie);
  137.             HttpGet httpGet =  new  HttpGet(rediretUrl);
  138.             httpGet.addHeader(“Accept”
  139.                     “text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,* / *; q = 0.8” );
  140.             // httpGet.addHeader(“Accept-Encoding”,“gzip,deflate,sdch”);
  141.             // httpGet.addHeader(“Accept-Language”,“zh-CN,zh; q = 0.8”);
  142.             httpGet.addHeader(“Connection” ,  “keep-alive” );
  143.             httpGet.addHeader(“Cookie” ,cookie);
  144.             httpGet.addHeader(“Host” ,  “mis.pyc.com.cn” );
  145.             httpGet.addHeader(“Referer” ,  “http://mis.pyc.com.cn/login.aspx” );
  146.             httpGet.addHeader(
  147.                     “用户代理”
  148.                     “ozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 40.0.2214.115 Safari / 537.36” );
  149.             response = client.execute(httpGet);
  150.             HttpEntity entity2 = response.getEntity();
  151.             System.out的
  152.                     .println(“———————————————- “ );
  153.             的System.out.println(EntityUtils.toString(ENTITY2));
  154.         }
  155.     }
  156. }
未经允许不得转载:比尔云 » HttpClient4.X的代理添加实现
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS