Apache Commons HttpClient 4.3.1改版

private static Header[] headers = {
   	new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"),   
    	new BasicHeader("Accept-Language", "zh-tw"),
    	new BasicHeader("Accept", " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"),   
    	new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8")};

public static void main(String[] args) {
	// for SSL : 2012-04-17
	try{
        TrustManager easyTrustManager = new X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
                //To change body of implemented methods use File | Settings | File Templates.
            }
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
                //To change body of implemented methods use File | Settings | File Templates.
            }
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                // return new java.security.cert.X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
                return null;
            }
        };

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[]{easyTrustManager}, null);
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

        CloseableHttpClient httpclient = HttpClients.custom()
        		.setRedirectStrategy(new LaxRedirectStrategy())
        		.setSSLSocketFactory(socketFactory)
        		.build();

        HttpPost httpost = new HttpPost();
        httpost.setHeaders(headers);

        HttpGet httpget = new HttpGet();
        httpget.setHeaders(headers);

        httpost.setURI(new URI( "https://localhost/j_security_check"  ));
        List  nvps = new ArrayList ();
        nvps.add(new BasicNameValuePair("j_username", "username"));
        nvps.add(new BasicNameValuePair("j_password", "password"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8") ));
	        
        HttpResponse response = httpclient.execute(httpost);
	        
        if (response.getEntity() != null){
        	String strResponse = EntityUtils.toString(response.getEntity());
        	EntityUtils.consume(response.getEntity());
        	// response.getEntity().consumeContent();
                // cookies = ((DefaultHttpClient)httpclient).getCookieStore().getCookies();
        	System.out.println(strResponse);
        }
      
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	} catch (KeyManagementException e) {
		e.printStackTrace();
	} catch (URISyntaxException e) {
		e.printStackTrace();
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	// for SSL : 2012-04-17
}
廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s