Search in sources :

Example 1 with BasicAuthenticator

use of com.cdeledu.common.httpEntity.BasicAuthenticator in project wechat by dllwh.

the class HttpURLConnHelper method initConn.

/**
 * @方法描述: 初始化连接相关信息的Post、Get访问请求参数设置
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年10月12日 上午7:53:09
 * @param url
 *            请求地址
 * @param RequestMethod
 *            请求方法
 * @param isUseProxy
 *            是否代理
 * @return
 */
private HttpURLConnection initConn(String url, String ReqMethod, boolean isUseProxy) throws Exception {
    if (StringUtils.isBlank(url)) {
        throw new RuntimeExceptionHelper("请求的URL不能为空");
    }
    HttpURLConnection httpConn = null;
    // 打开HttpURLConnection
    URL realUrl = new URL(url);
    if (isUseProxy) {
        Map<String, Object> ipMap = IpUtilHelper.getProxyIp();
        // http访问要使用的代理服务器的地址
        String proxyIp = String.valueOf(ipMap.get("proxyIp"));
        // http访问要使用的代理服务器的端口
        String proxyPort = String.valueOf(ipMap.get("proxyPort"));
        // http访问要使用的代理服务器的用户名
        String userName = String.valueOf(ipMap.get("userName"));
        // http访问要使用的代理服务器的密码
        String password = String.valueOf(ipMap.get("proxyPort"));
        Proxy newProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort)));
        if (StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password)) {
            Authenticator.setDefault(new BasicAuthenticator(userName, password));
        }
        urlConn = realUrl.openConnection(newProxy);
    } else {
        urlConn = realUrl.openConnection();
    }
    /**
     * 设置http头通用的请求属性
     */
    urlConn.setRequestProperty(HttpHeaders.ACCEPT, "*/*");
    urlConn.setRequestProperty(HttpHeaders.CONNECTION, "Keep-Alive");
    // 请求表示提交内容类型或返回返回内容的MIME类型
    urlConn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    // 设置 HttpURLConnection的字符编码,从而避免出现乱码
    urlConn.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, URLCHARSET);
    // 浏览页面的访问者在用什么操作系统(包括版本号)、浏览器(包括版本号)等
    urlConn.setRequestProperty(HttpHeaders.USER_AGENT, UserAgentType.PC_Firefox.getValue());
    boolean useHttps = url.startsWith("https");
    if (useHttps) {
        HttpsURLConnection httpsConn = (HttpsURLConnection) realUrl.openConnection();
        // 创建SSLContext对象,并使用我们指定的信任管理器初始化
        TrustManager[] tm = { new MyX509TrustManager() };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        httpsConn.setSSLSocketFactory(ssf);
        httpConn = (HttpsURLConnection) httpsConn;
    } else {
        httpConn = (HttpURLConnection) urlConn;
    }
    boolean caches = true;
    /**
     * 发送POST请求必须设置如下所示
     */
    if ("POST".equalsIgnoreCase(ReqMethod)) {
        /**
         * 1.设置是否向httpUrlConnection输出,默认情况下是false<BR/>
         * 2.是否打开输出流 true|false,表示向服务器写数据
         */
        httpConn.setDoOutput(true);
        /**
         * 1.设置是否从httpUrlConnection读入,默认情况下是true<br/>
         * 2.是否打开输入流true|false,表示从服务器获取数据
         */
        httpConn.setDoInput(true);
        /**
         * 1.是否缓存true|false<BR/>
         * 2.Post 请求不能使用缓存
         */
        caches = false;
    }
    // 是否缓存true|false
    httpConn.setUseCaches(caches);
    // 设置连接超时时间,单位毫秒
    httpConn.setConnectTimeout(10 * 1000);
    // 设置读取数据超时时间,单位毫秒
    httpConn.setReadTimeout(60 * 1000);
    // 设置 HttpURLConnection的请求方式-->POST|GET,默认是GET
    if (POST_HTTP.equalsIgnoreCase(ReqMethod)) {
        httpConn.setRequestMethod(POST_HTTP);
    } else if (GET_HTTP.equalsIgnoreCase(ReqMethod)) {
        httpConn.setRequestMethod(GET_HTTP);
    }
    /**
     * 参数配置必须要在connect之前完成
     */
    return httpConn;
}
Also used : RuntimeExceptionHelper(com.cdeledu.common.exception.RuntimeExceptionHelper) InetSocketAddress(java.net.InetSocketAddress) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) MyX509TrustManager(com.cdeledu.common.network.MyX509TrustManager) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) BasicAuthenticator(com.cdeledu.common.httpEntity.BasicAuthenticator) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MyX509TrustManager(com.cdeledu.common.network.MyX509TrustManager)

Aggregations

RuntimeExceptionHelper (com.cdeledu.common.exception.RuntimeExceptionHelper)1 BasicAuthenticator (com.cdeledu.common.httpEntity.BasicAuthenticator)1 MyX509TrustManager (com.cdeledu.common.network.MyX509TrustManager)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 URL (java.net.URL)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 TrustManager (javax.net.ssl.TrustManager)1