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("[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,看起来更简单的样子。

时间: 2024-07-29 12:36:00

Apache HttpClient POST数据(https)的相关文章

使用Apache HttpClient 4.x发送Json数据

Apache HttpClient是Apache提供的一个开源组件,使用HttpClient可以很方便地进行Http请求的调用.自4.1版本开始,HttpClient的API发生了较大的改变,很多方法的调用方式已经和3.x版本不同.本文使用的是当前最新的4.5.3版本. 首先在pom文件中引入httpcomponents依赖包: 1 <dependency> 2 <groupId>org.apache.httpcomponents</groupId> 3 <art

Apache HttpClient : Http Cookies

前言 HttpClient已经被集成到Android的SDK里,但在JDK里面仍然需要HttpURLConnectionn发起HTTP请求.HttpClient可以看做是一个加强版的HttpURLConnection,但它的侧重点是如何发送请求.接受相应和管理Http连接. 在介绍Http Cookies之前,笔者给出一个应用场景:你需要一个根据地理信息(城市名或者经纬度)获取天气的应用.可选的API很多,不幸的是,网上提到的Google天气API已经停止服务了(不是被墙):雅虎是英文的,且需要

Android(java)学习笔记211:采用httpclient提交数据(qq登录案例)

1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议.(类比推理MySQL数据库客户端) 代码模拟浏览器的行为              (1)打开浏览器              (2)输入数据              (3)敲回车 2.Httpclient使用GET方式提交数据: 其中MainActi

android使用apache httpclient发送post请求

package com.liuc; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.ht

Apache HttpClient访问网络工具类

1 package com.ztravel.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org

新旧apache HttpClient 获取httpClient方法

在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHttpClientHttpResponse -> CloseableHttpResponse 目前互联网对外提供的接口通常都是HTTPS协议,有时候接口提供方所示用的证书会出现证书不受信任的提示,chrome访问接口(通常也不会用chrome去访问接口,只是举个例子)会出现这样的提示: 为此我们调用这类接口

Android 6.0 使用 Apache HttpClient

Android 6.0版本已经已经基本将Apahce Http Client 移除出SDK. 那么问题来了,如果我在以前的项目中使用了Apache HttpClient相关类,怎么办呢? 请看官网给出的答案 Apache HTTP Client Removal Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.

Apache配置SSL 实现https访问

本次坏境:CA和apache为同一台主机 先使本机作为CA服务端: [[email protected]~]#yum -y install openssl openssl-devel [[email protected]~]#vi /etc/pki/tls/openssl.cnf [ CA_default ] dir = ../../CA 改为: [ CA_default ] dir= /etc/pki/CA 为了减少不必要的重复操作,可以预先定义[ req_distinguished_name

使用 Apache Pig 处理数据5

使用 Apache Pig 从大数据集中获得所需的信息 Apache Pig 是一个高级过程语言,适合于使用 Hadoop 和 MapReduce 平台来查询大型半结构化数据集.通过允许对分布式数据集进行类似 SQL 的查询,Pig 可以简化 Hadoop 的使用.本文将探索 Pig 背后的语言,并在一个简单的 Hadoop 集群中发现其用途. Hadoop 的普及和其生态系统的不断壮大并不令人感到意外.Hadoop 不断进步的一个特殊领域是 Hadoop 应用程序的编写.虽然编写 Map 和