HttpClient

package com.hzwealth.app.clic;

 

import java.io.IOException;

import java.net.URLEncoder;

 

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.UsernamePasswordCredentials;

import org.apache.commons.httpclient.auth.AuthScope;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.http.client.methods.HttpGet;

 

 

public class TestWS {

 

/**

 * get请求

 * @throws HttpException

 * @throws IOException

 */

public void testGet() throws HttpException, IOException{

//创建客户端,类似于打开浏览器

HttpClient httpClient=new HttpClient();

//创建GET方法,类似于输入浏览器地址

GetMethod getMethod=new GetMethod(

"http://tianxingske.com/api/rest/police/identity?account=hzcf&accessToken=f0bf3250a3504b4aad9aff0dc42aac88" +

"&name="+URLEncoder.encode("昌易梦", "utf-8")+"&idCard=451123198612281167");

//回车获取响应状态码

int statusCode= httpClient.executeMethod(getMethod);

System.out.println("statusCode="+statusCode);

//查看命中情况,可以获取的东西还有很多 比如 head cookies等

System.out.println("response="+getMethod.getResponseBodyAsString());

/*String response = new String(getMethod.getResponseBodyAsString().getBytes("gb2312"));

System.out.println("response=========="+response);*/

//释放资源

getMethod.releaseConnection();

}

/**

 * post请求

 * @throws HttpException

 * @throws IOException

 */

public void testPost() throws HttpException, IOException{

//创建客户端,类似于打开浏览器

HttpClient httpClient=new HttpClient();

//创建post方法,类似于输入浏览器地址(百度会出现302)

PostMethod postMethod=new PostMethod("http://tianxingshuke.com/api/rest/common/organization/auth");

//使用数组来传递参数

NameValuePair[] postPair=new NameValuePair[2];

postPair[0]= new NameValuePair("account", "hzcf");

postPair[1]=new NameValuePair("signature","197a71867184484191fa49e3216b6ca6");

postMethod.addParameters(postPair);

//回车获取响应状态码

int statusCode= httpClient.executeMethod(postMethod);

System.out.println("statusCode="+statusCode);

System.out.println("url="+postMethod.getURI());

//查看命中情况,可以获取的东西还有很多 比如 head cookies等

System.out.println("response="+postMethod.getResponseBodyAsString());

//释放资源

postMethod.releaseConnection();

}

/**

 * 代理

 * @throws HttpException

 * @throws IOException

 */

public void testProxy() throws HttpException, IOException{

String ip="192.168.0.1";

int port=9527;

//创建客户端,类似于打开代理

HttpClient httpClient=new HttpClient();

//设置代理服务器的ip和端口

httpClient.getHostConfiguration().setProxy(ip, port);

//先告诉httpclicent抢先认证,否者你会收到"你没有资格"的结果

httpClient.getParams().setAuthenticationPreemptive(true);

//MyProxyCredentialsProvider 返回代理的Credential(userName/password)

httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new MyProxyCredentialsProvider());

//设置代理服务器的用户名和密码

httpClient.getState().setProxyCredentials(new AuthScope(ip,AuthScope.ANY_PORT,AuthScope.ANY_REALM), 

new UsernamePasswordCredentials("username","password"));

//创建post方法,类似于输入浏览器地址(百度会出现302)

PostMethod postMethod=new PostMethod("http://www.baidu.com/");

//使用数组来传递参数

NameValuePair[] postPair=new NameValuePair[2];

postPair[0]= new NameValuePair("武器", "枪");

postPair[1]=new NameValuePair("什么枪","狙击枪");

postMethod.addParameters(postPair);

//回车获取响应状态码

int statusCode= httpClient.executeMethod(postMethod);

System.out.println("statusCode="+statusCode);

System.out.println("url="+postMethod.getURI());

//查看命中情况,可以获取的东西还有很多 比如 head cookies等

System.out.println("response="+postMethod.getResponseBodyAsString());

//释放资源

postMethod.releaseConnection();

}

public static void main(String[] args) {

try {

//new TestWS().testPost();

new TestWS().testGet();

} catch (HttpException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值