对Java的HttpURLConnection的封装类

作者: Luan 分类: 编程开发 发布时间: 2017-03-26 00:27

百度到处搜搜,整合在一起.

以后用Java写工具如果不需要高效率就直接用这个类了。

另外还有个Bug就是如果写扫描器,GET获取个大文件会卡住,懒得弄了,有空了再看吧。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class Luan_http {
    private String boundary = "--------WebScanByLuan";
    private HashMap<String, String> textParams = new HashMap<String, String>();
    private HashMap<String, byte[]> fileparams = new HashMap<String, byte[]>();
    private DataOutputStream ds;
    
    private URL url;
    private String method = "GET";
    private String agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0";
    private String fake_ip = "";
    private String proxy_ip = "";
    private int proxy_port = 8080;
    private int timeout = 3000;
    private boolean readHtml = true;
    
    private int httpCode = 0;
    private int contentLength = 0;
    private byte[] response = null;
    private HashMap<String, String> headers = null;
    public Luan_http(){
    }
    public Luan_http(String url) throws Exception {
        this.url = new URL(url);
    }
    public Luan_http(String url,String method,String agent,String fake_ip,String proxy_ip,int proxy_port,int timeout,boolean readHtml) throws Exception {
    	setUrl(url);
    	setMethod(method);
    	setAgent(agent);
    	setFakeIp(fake_ip);
    	setProxy(proxy_ip,proxy_port);
    	setTimeout(timeout);
    	setReadHtml(readHtml);
    }
    public void setUrl(String url) throws Exception {
        this.url = new URL(url);
    }
    public void setMethod(String method){
        this.method = method;
    }
    public void setAgent(String agent){
        this.agent = agent;
    }
    public void setFakeIp(String fake_ip){
        this.fake_ip = fake_ip;
    }
    public void setProxy(String proxy_ip,int proxy_port){
        this.proxy_ip = proxy_ip;
        this.proxy_port = proxy_port;
    }
    public void setProxyip(String proxy_ip){
        this.proxy_ip = proxy_ip;
    }
    public void setProxyport(int proxy_port){
        this.proxy_port = proxy_port;
    }
    public void setTimeout(int timeout){
        this.timeout = timeout;
    }
    public void setReadHtml(boolean readHtml){
        this.readHtml = readHtml;
    }
    public void addTextParameter(String name, String value) {
        textParams.put(name, value);
    }
    public void addFileParameter(String name, String filename, String type, byte[] value) {
        fileparams.put(name+">>WebScan<<"+filename+">>WebScan<<"+type, value);
    }
    public int getHttpCode() {
        return httpCode;
    }
    public int getContentLength() {
        return contentLength;
    }
    public byte[] getResponse() {
        return response;
    }
    public HashMap<String, String> getHeaders() {
        return headers;
    }
    public String getHeader(String key) {
        return headers.get(key);
    }
    public void clear() {
        textParams.clear();
        fileparams.clear();
        response = null;
        httpCode = 0;
        contentLength = 0;
    }
    public boolean send() throws Exception {
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        HttpURLConnection conn;
    	if(proxy_ip == ""){
    		conn = (HttpURLConnection) this.url.openConnection();
    	}else{
    		Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy_ip, proxy_port));
    		conn = (HttpURLConnection) this.url.openConnection(proxy);
    	}
    	// conn.setRequestProperty("Accept-Encoding", "identity");
    	/*
    	 * 如果网站支持gzip压缩,HttpURLConnection就会和服务器使用gzip,getContentLength()返回值就会是-1
    	 * 另外如果文件本身是.gz,好像也会返回-1 !!
    	 * 也就是说如果请求的是一个大文件。就得注意下了。
    	 * 参考:
    	 * By default, this implementation of HttpURLConnection requests that servers use gzip compression. 
    	 * Since getContentLength() returns the number of bytes transmitted, 
    	 * you cannot use that method to predict how many bytes can be read from getInputStream(). 
    	 * Instead, read that stream until it is exhausted: whenread() returns -1. 
    	 */
    	conn.setDoOutput(true);
        //conn.setUseCaches(false);
        conn.setConnectTimeout(timeout);
        conn.setRequestMethod(method);
        conn.setRequestProperty("User-Agent",agent);
        if(fake_ip != "")
        	conn.setRequestProperty("X-Forwarded-Ror",fake_ip);
        if(method == "POST"){
        	conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
        	ds = new DataOutputStream(conn.getOutputStream());
            Set<String> keySet = textParams.keySet();  
            for (Iterator<String> it = keySet.iterator(); it.hasNext();) {  
                String name = it.next();  
                String value = textParams.get(name);  
                ds.writeBytes("--" + boundary + "\r\n");  
                ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
                        + "\"\r\n");  
                ds.writeBytes("\r\n");  
                ds.writeBytes(encode(value) + "\r\n");  
            }
            keySet = fileparams.keySet();  
            for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
            	String a = it.next();
                String[] data = a.split(">>WebScan<<");
                String name = data[0],filename = data[1],type = data[2];
                ds.writeBytes("--" + boundary + "\r\n");  
                ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
                        + "\"; filename=\"" + encode(filename) + "\"\r\n");  
                ds.writeBytes("Content-Type: " + type + "\r\n");
                ds.writeBytes("\r\n");
                ds.write(fileparams.get(a));
                ds.writeBytes("\r\n");
            }
            ds.writeBytes("--" + boundary + "--" + "\r\n");
            ds.writeBytes("\r\n");
        }
        //conn.setRequestProperty("Connection", "Keep-Alive");
        try {
        	conn.setConnectTimeout(timeout);
            conn.connect();
	        //BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	        httpCode = conn.getResponseCode();
	        Map<String,List<String>>  tmpMap = conn.getHeaderFields();
	        headers = new HashMap<String, String>();
	        for(Entry<String, List<String>> entry:tmpMap.entrySet()){
	            String value = "";
	            for(String _value : entry.getValue()){
	            	value += _value + ";";
	            }
	            headers.put(entry.getKey(), value.substring(0,value.length()-1));
	        }
	        // Get HTML
	        if(!conn.getRequestMethod().toUpperCase().equals("HEAD") && readHtml){
	        	InputStream in;
	            try{
	            	in = conn.getInputStream();
	            }catch(Exception ex){
	            	in = conn.getErrorStream();
	            }
	        	ByteArrayOutputStream out = new ByteArrayOutputStream();
	            int b;
	            while ((b = in.read()) != -1) {
	                out.write(b);
	            }
	            response = out.toByteArray();
	        }
	        conn.disconnect();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    private String encode(String value) throws Exception{
        return URLEncoder.encode(value, "UTF-8");
    }
    
    
    
    
    
    
    
    HostnameVerifier hv = new HostnameVerifier() {  
        public boolean verify(String urlHostName, SSLSession session) {  
            System.out.println("Warning: URL Host: " + urlHostName + " vs. "  
                               + session.getPeerHost());  
            return true;  
        }  
    };  
   
 private static void trustAllHttpsCertificates() throws Exception {  
 javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];  
 javax.net.ssl.TrustManager tm = new miTM();  
 trustAllCerts[0] = tm;  
 javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext  
 .getInstance("SSL");  
 sc.init(null, trustAllCerts, null);  
 javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc  
 .getSocketFactory());  
 }  
  
 static class miTM implements javax.net.ssl.TrustManager,  
 javax.net.ssl.X509TrustManager {  
 public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
 return null;  
 }  
  
 public boolean isServerTrusted(  
 java.security.cert.X509Certificate[] certs) {  
 return true;  
 }  
  
 public boolean isClientTrusted(  
 java.security.cert.X509Certificate[] certs) {  
 return true;  
 }  
  
 public void checkServerTrusted(  
 java.security.cert.X509Certificate[] certs, String authType)  
 throws java.security.cert.CertificateException {  
 return;  
 }  
  
 public void checkClientTrusted(  
 java.security.cert.X509Certificate[] certs, String authType)  
 throws java.security.cert.CertificateException {  
 return;  
 }  
 }  
}

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论