Apache HttpClient POST資料(https)

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   os   使用   sp   

測試用的httpclient版本

<dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.1.2</version>    <scope>test</scope></dependency>

1.傳索引值對

http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

2.發送https請求
http://javaskeleton.blogspot.it/2010/07/avoiding-peer-not-authenticated-with.html

最終測試代碼:

public class LoginTest {    @Test    public void testHttpPost() throws Exception {        HttpClient client = new DefaultHttpClient();        client = WebClientDevWrapper.wrapClient(client);        HttpPost post = new HttpPost("https://localhost:8443/login");//        StringEntity entity = new StringEntity("[email protected]&pwd=111&type=x");//        post.setEntity(entity);        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);        nameValuePairs.add(new BasicNameValuePair("user", "[email protected]"));        nameValuePairs.add(new BasicNameValuePair("pwd", "111"));        nameValuePairs.add(new BasicNameValuePair("type", "x"));        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));        HttpResponse res = client.execute(post);        System.out.println(res.getStatusLine());        BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));        String line = br.readLine();        while (line != null) {            System.out.println(line);            line = br.readLine();        }        client.getConnectionManager().shutdown();    }}

工具類:

/*This code is public domain: you are free to use, link and/or modify it in any way you want, for all purposes including commercial applications. */public class WebClientDevWrapper {    public static HttpClient wrapClient(HttpClient base) throws Exception {        SSLContext ctx = SSLContext.getInstance("TLS");        X509TrustManager tm = new X509TrustManager() {            @Override            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {            }            @Override            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {            }            @Override            public java.security.cert.X509Certificate[] getAcceptedIssuers() {                return new java.security.cert.X509Certificate[0];            }        };        ctx.init(null, new TrustManager[]{tm}, null);        SSLSocketFactory ssf = new SSLSocketFactory(ctx);        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);        ClientConnectionManager ccm = base.getConnectionManager();        SchemeRegistry sr = ccm.getSchemeRegistry();        sr.register(new Scheme("https", ssf, 443));        return new DefaultHttpClient(ccm, base.getParams());    }}

 使用命令列測試:

curl -k -X POST https://localhost:8443/login --data "[email protected]&pwd=111&type=x"

待研究http://my.oschina.net/wenziqiu/blog/339630,看起來更簡單的樣子。


Apache HttpClient POST資料(https)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.