HttpClient解析页面总结

1、遇到安全控件证书(单向和双向认证)

  
  
  
  
  1. //安全证书的处理  
  2.         Protocol myhttps = new Protocol("https",  
  3.                 new MySecureProtocolSocketFactory(), 443);  
  4.         Protocol.registerProtocol("https", myhttps); 

 

  
  
  
  
  1. package com.bwzy.jiaotongyunguan.util;  
  2.  
  3. import java.io.IOException;  
  4. import java.net.InetAddress;  
  5. import java.net.InetSocketAddress;  
  6. import java.net.Socket;  
  7. import java.net.SocketAddress;  
  8. import java.net.UnknownHostException;  
  9. import java.security.KeyManagementException;  
  10. import java.security.NoSuchAlgorithmException;  
  11. import java.security.cert.CertificateException;  
  12. import java.security.cert.X509Certificate;  
  13.  
  14. import javax.net.SocketFactory;  
  15. import javax.net.ssl.SSLContext;  
  16. import javax.net.ssl.TrustManager;  
  17. import javax.net.ssl.X509TrustManager;  
  18.  
  19. import org.apache.commons.httpclient.ConnectTimeoutException;  
  20. import org.apache.commons.httpclient.params.HttpConnectionParams;  
  21. import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;  
  22.  
  23. public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {  
  24.     static{  
  25.         System.out.println(">>>>in MySecureProtocolSocketFactory>>");  
  26.     }  
  27.     private SSLContext sslcontext = null;  
  28.       
  29.     private SSLContext createSSLContext() {  
  30.         SSLContext sslcontext=null;  
  31.         try {  
  32.             sslcontext = SSLContext.getInstance("SSL");  
  33.             sslcontext.init(nullnew TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());  
  34.         } catch (NoSuchAlgorithmException e) {  
  35.             e.printStackTrace();  
  36.         } catch (KeyManagementException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.         return sslcontext;  
  40.     }  
  41.       
  42.     private SSLContext getSSLContext() {  
  43.         if (this.sslcontext == null) {  
  44.             this.sslcontext = createSSLContext();  
  45.         }  
  46.         return this.sslcontext;  
  47.     }  
  48.       
  49.     public Socket createSocket(Socket socket, String host, int port, boolean autoClose)  
  50.             throws IOException, UnknownHostException {  
  51.         return getSSLContext().getSocketFactory().createSocket(  
  52.                 socket,  
  53.                 host,  
  54.                 port,  
  55.                 autoClose  
  56.             );  
  57.     }  
  58.  
  59.     public Socket createSocket(String host, int port) throws IOException,  
  60.             UnknownHostException {  
  61.         return getSSLContext().getSocketFactory().createSocket(  
  62.                 host,  
  63.                 port  
  64.             );  
  65.     }  
  66.       
  67.       
  68.     public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)  
  69.             throws IOException, UnknownHostException {  
  70.         return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);  
  71.     }  
  72.  
  73.     public Socket createSocket(String host, int port, InetAddress localAddress,  
  74.             int localPort, HttpConnectionParams params) throws IOException,  
  75.             UnknownHostException, ConnectTimeoutException {  
  76.         if (params == null) {  
  77.             throw new IllegalArgumentException("Parameters may not be null");  
  78.         }  
  79.         int timeout = params.getConnectionTimeout();  
  80.         SocketFactory socketfactory = getSSLContext().getSocketFactory();  
  81.         if (timeout == 0) {  
  82.             return socketfactory.createSocket(host, port, localAddress, localPort);  
  83.         } else {  
  84.             Socket socket = socketfactory.createSocket();  
  85.             SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);  
  86.             SocketAddress remoteaddr = new InetSocketAddress(host, port);  
  87.             socket.bind(localaddr);  
  88.             socket.connect(remoteaddr, timeout);  
  89.             return socket;  
  90.         }  
  91.     }  
  92.       
  93.     //自定义私有类  
  94.     private static class TrustAnyTrustManager implements X509TrustManager {  
  95.          
  96.         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  97.         }  
  98.      
  99.         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  100.         }  
  101.      
  102.         public X509Certificate[] getAcceptedIssuers() {  
  103.             return new X509Certificate[]{};  
  104.         }  
  105.     }  
  106.       
  107.  

所需要的jar包有commons-httpclient-3.1.jar、 commons-codec-1.7.jar 、httpcore.jar、commons-logging.jar

HttpClient相当于一个浏览器(但不支持javascript脚本)

//创建一个HttpClient

HttpClient client = new HttpClient();

//已某种方式发出请求(7中请求方式包括:Get、post、head……),接下来以Get和post来说明问题。

//创建一个请求方式

GetMethod get = new GetMethod(url);

//其中Url是访问的网址(解析的目标),如果你解析百度首页String url="http://www.baidu.com"

//通过client的该方法来发出请求,参数是请求的方式。

client.executeMethode(get);

//执行之后调用method的方法来获取client的响应。

get.getResponseBodyAsString();

//这样就可以得到百度页面的HTML代码,你可以从代码中获取你所需要的数据。

//如果你的请求指向的是一个文件,如word文档。你可以以流的方式获取。

get.getResponseBodyAsStream();

//请求方式有一些方法,能获取和修改请求头和相应头的信息,还可以设置一些编码方式。接下来是一段获取流的代码。

  
  
  
  
  1. get.setURI(new URI(url, false"gb2312"));  
  2.             httpClient.executeMethod(get);  
  3.             InputStream in = get.getResponseBodyAsStream();  
  4.             ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  5.             int b = 0;  
  6.             while ((b = in.read()) != -1) {  
  7.                 outStream.write(b);  
  8.             }  
  9.             byte[] bytes = outStream.toByteArray();  
  10.             long tempLong = System.currentTimeMillis();  
  11.             String[] paths = PdfConverterServlet.class.getClassLoader().getResource("/").getPath().split("/");  
  12.             String path ="";  
  13.             for (int i = 1; i < paths.length-2; i++) {  
  14.                 path+=paths[i]+"/";  
  15.             }  
  16.             System.out.println(path);  
  17.             OutputStream out = new FileOutputStream(new File(path+"fileTemp/",  
  18.                     tempLong + ".doc"));  
  19.             out.write(bytes);  
  20.             out.close(); 

Post方式请求

大家知道,Get和Post在参数上有区别,Get方式的请求可以直接在链接地址后面跟上参数列表,而Post却不能。那么Post如何传参呢?

 

  
  
  
  
  1. post = new PostMethod(url);  
  2.             NameValuePair[] param = {  
  3.                     new NameValuePair("actionflag", (String) params  
  4.                             .get("actionflag")),  
  5.                     new NameValuePair("userlogid", (String) params  
  6.                             .get("userlogid")),  
  7.                     new NameValuePair("isdeliverdo", (String) params  
  8.                             .get("isdeliverdo")),  
  9.                     new NameValuePair("editFlag", (String) params  
  10.                             .get("editFlag")),  
  11.                     new NameValuePair("isflow", (String) params.get("isflow")),  
  12.                     new NameValuePair("workid", (String) params.get("workid")),  
  13.                     new NameValuePair("flowlogid", (String) params  
  14.                             .get("flowlogid")),  
  15.                     new NameValuePair("jflowobjectno", (String) params  
  16.                             .get("jflowobjectno")),  
  17.                     new NameValuePair("actionfrom", (String) params  
  18.                             .get("actionfrom")),  
  19.                     new NameValuePair("scopeids", (String) params  
  20.                             .get("scopeids")),  
  21.                     new NameValuePair("biaoti", (String) params.get("biaoti")),  
  22.                     new NameValuePair("chaosong", (String) params  
  23.                             .get("chaosong")),  
  24.                     new NameValuePair("deptname", (String) params  
  25.                             .get("deptname")),  
  26.                     new NameValuePair("fengfariqicn", (String) params  
  27.                             .get("fengfariqicn")),  
  28.                     new NameValuePair("fenshu", (String) params.get("fenshu")),  
  29.                     new NameValuePair("miji", (String) params.get("miji")),  
  30.                     new NameValuePair("nigaobumen", (String) params  
  31.                             .get("nigaobumen")),  
  32.                     new NameValuePair("nigaobumenlogin", (String) params  
  33.                             .get("nigaobumenlogin")),  
  34.                     new NameValuePair("nigaobumenid", (String) params  
  35.                             .get("nigaobumenid")),  
  36.                     new NameValuePair("nigaoren", (String) params  
  37.                             .get("nigaoren")),  
  38.                     new NameValuePair("nigaoriqi", (String) params  
  39.                             .get("nigaoriqi")),  
  40.                     new NameValuePair("zhusong", URLDecoder  
  41.                             .decode((String) params.get("zhusong"))), };  
  42.             System.out.println();  
  43.             post.addParameters(param);  
  44.             httpClient.executeMethod(post);  
  45.             doc = Jsoup.parse(post.getResponseBodyAsString()); 

 

你可能感兴趣的:(认证,安全证书)