使用URLConnection进行访问

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

public class HttpUtil {
    
    private static Logger logger = Logger.getLogger(HttpUtil.class);
    private String DEFAULT_CHARSET = "UTF-8";
    private String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded";
    
    public void setCharSet(String charSet){
        this.DEFAULT_CHARSET = charSet;
    }
    
    public void setContentType(String contentType){
        this.DEFAULT_CONTENT_TYPE = contentType;
    }
    
    public String post(String urlStr, Map<String, String> paramMap){
        return post(urlStr, paramMap, null);
    }
    
    
    public String post(String urlStr, Map<String, String> paramMap, Map<String, Object> proxyMap){
         Map<String, String> formatUrlMap = formatUrlStr(urlStr);
         if(formatUrlMap==null){
             return "";
         }
         
         urlStr = formatUrlMap.get("url");
         logger.info("url请求地址:"+urlStr);
         String urlParam = formatUrlMap.get("param");
         
         String allParam = formatParam(urlParam, paramMap);
         logger.info("整合得到参数串:"+allParam);
         
         return post(urlStr, allParam, proxyMap);
    }
    
    public String post(String urlStr, String paramStr, Map<String, Object> proxyMap){
        URL url = null;
        URLConnection urlConnection = null;
        try {
            url = new URL(urlStr);
            
            if(proxyMap!=null&&proxyMap.size()>0&&proxyMap.get("proxy")!=null){
                Proxy proxy = (Proxy)proxyMap.get("proxy");
                urlConnection = url.openConnection(proxy);
                logger.info("使用代理模式连接");
            }else{
                urlConnection = url.openConnection();
            }
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        if(urlConnection==null){
            logger.info("创建url连接失败");
            return "";
        }
        
        if(urlStr.startsWith("https")){
            logger.info("请求地址是https类型,进行设置");
            SSLSocketFactory sslSocketFactory = null;
            try {
                SSLContext sslContext = SSLContext.getInstance("TLS");
                TrustManager[] trustManagers = initTrustManager();
                sslContext.init(null, trustManagers, new SecureRandom());
                sslSocketFactory = sslContext.getSocketFactory();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            }
            
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)urlConnection;
            httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
            httpsURLConnection.setHostnameVerifier(initHostnameVerifier());
        }
        
        HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
        //如果有代理,查看是否有账号密码授权,进行授权
        if(proxyMap!=null&&proxyMap.size()>0&&proxyMap.get("proxy")!=null){
            Object accountObj = proxyMap.get("account");
            Object passwordObj = proxyMap.get("password");
            
            String account = "";
            if(accountObj!=null){
                account = (String)accountObj;
            }
            String password = "";
            if(passwordObj!=null){
                password = (String)passwordObj;
            }
            
            if(!"".equals(account)&&!"".equals(password)){
                logger.info("代理授权账号和密码:"+account+" / "+password);
                String headerKey = "Proxy-Authorization";
                BASE64Encoder base64Encoder = new BASE64Encoder();
                String headerVal = "Basic "+base64Encoder.encode((account+":"+password).getBytes());
                httpURLConnection.setRequestProperty(headerKey, headerVal);
            }
        }
        
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setConnectTimeout(5000);
        httpURLConnection.setReadTimeout(5000);
        httpURLConnection.setRequestProperty("accept", "*/*");
        httpURLConnection.setRequestProperty("connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");
        
        //requestMethod默认POST
        try {
            httpURLConnection.setRequestMethod("POST");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        httpURLConnection.setRequestProperty("Content-type",DEFAULT_CONTENT_TYPE+";charset="+DEFAULT_CHARSET);
        
        if(paramStr==null){
            paramStr = "";
        }
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(paramStr.length()));
        
        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        OutputStream os = null;
        try {
            os = httpURLConnection.getOutputStream();
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
            bw.write(paramStr);
            bw.flush();
            
            httpURLConnection.connect();
            logger.info("连接开启");
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(bw!=null){
                    bw.close();
                }
                if(osw!=null){
                    osw.close();
                }
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        String result = null;
        BufferedReader br = null;
        InputStreamReader isr = null;
        InputStream is = null;
        try {
            int responseCode = httpURLConnection.getResponseCode();
            logger.info("返回码:"+responseCode);
            if(responseCode==200){
                is = httpURLConnection.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                
                StringBuilder sb = new StringBuilder();
                
                String temp = null;
                while((temp=br.readLine())!=null){
                    sb.append(temp);
                }
                
                result = sb.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(br!=null){
                    br.close();
                }
                if(isr!=null){
                    isr.close();
                }
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        httpURLConnection.disconnect();
        logger.info("连接关闭");
        
        return result;
    }
    
    /**
     * 添加代理
     * @param hostname
     * @param port
     * @param account
     * @param password
     * @return
     */
    public Map<String, Object> addProxy(String hostname,int port,String account,String password){
        Proxy proxy = null;
        try {
            InetSocketAddress address = new InetSocketAddress(hostname, port);
            proxy = new Proxy(Proxy.Type.HTTP, address);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加代理异常");
        }
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("proxy", proxy);
        map.put("account", account);
        map.put("password", password);
        return map;
    }
    
    
    private static Map<String, String> formatUrlStr(String urlStr){
        Map<String, String> map = new HashMap<String, String>();
        
        if(urlStr==null||urlStr.trim().length()==0){
            logger.warn("URL为空");
            return null;
        }
        
        String param = null;
        if(urlStr.contains("?")){
            int urlEndIndex = urlStr.indexOf("?");
            param = urlStr.substring(urlEndIndex+1);
            urlStr = urlStr.substring(0, urlEndIndex);
        }
        
        if(!urlStr.startsWith("http")){
            if(urlStr.startsWith("//")){
                urlStr = "http:" + urlStr;
            }else{
                urlStr = "http://" + urlStr;
            }
        }
        
        map.put("url", urlStr);
        map.put("param", param);
        return map;
    }
    
    
    private static String formatParam(String param,Map<String, String> paramMap){
        Map<String, String> m = null;
        if(paramMap==null){
            m = new HashMap<String, String>();
        }else{
            m = paramMap;
        }
        
        if(param!=null&&param.length()>0){
            String[] keyAndValues = param.split("&");
            if(keyAndValues!=null&&keyAndValues.length>0){
                for(String keyAndValue :keyAndValues){
                    String[] arr = keyAndValue.split("=",2);
                    if(arr==null||arr.length==0){
                        continue;
                    }
                    String key = arr[0];
                    String value = arr[1];
                    
                    m.put(key, value);
                }
            }
        }
        
        if(m==null||m.size()==0){
            return "";
        }
        
        String rs = "";
        
        for(String key :m.keySet()){
            String value = m.get(key);
            
            rs += (key+"="+value+"&");
        }
        
        return rs;
    }
    
    
    private static TrustManager[] initTrustManager(){
        TrustManager X509TrustManager = new X509TrustManager() {
            
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            
            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
                
            }
            
            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
                
            }
        };
        
        
        TrustManager[] trustManagers = {X509TrustManager};
        
        return trustManagers;
    }
    
    private static HostnameVerifier initHostnameVerifier(){
        return new HostnameVerifier() {
            
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
    }
    
}

 

转载于:https://www.cnblogs.com/jinzhiming/p/8533869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值