Apache HttpClient POST数据(https)

测试用的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("user=test@abc.com&pwd=111&type=x");
//        post.setEntity(entity);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", "test@abc.com"));
        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 "user=test@abc.com&pwd=111&type=x"

待研究http://my.oschina.net/wenziqiu/blog/339630,看起来更简单的样子。


转载于:https://my.oschina.net/uniquejava/blog/354355

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值