java webclient jar_【commons-httpclient】Java中HttpClient工具访问Web请求

注意jar包是:

7043d9c9dc5cc34f13e77d3515cada1a.png

HttpClient工具使用

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

为什么要使用HttpClient工具:

原生态的Socket基于传输层,现在我们要访问的WebService是基于HTTP的属于应用层,所以我们的Socket通信要借助HttpClient发HTTP请求,这样格式才能匹配

HttpClient使用步骤如下:

创建 HttpClient 的实例

创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址

配置要传输的参数,和消息头信息

调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例

通过response读取字符串

释放连接。无论执行方法是否成功,都必须释放连接

jar包:

2QY0gGSAT64UWH+hw7uikm1PV60DoGv4IKEgA9udJhPUKSPj6jt8aIljQMPXZAg8MEFAJgHzk8AAPNAWQAA5vkfbZ3TCkOdc6gAAAAASUVORK5CYII=

1。第一种使用方式:

Get方式:

public static void getMethod() throwsException {//创建get对象,类似get请求

GetMethod getMethod = new GetMethod( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=18373551982&userID=");//发送get请求

int code =http.executeMethod(getMethod);

System.out.println("返回的消息码为:" +code);

System.out.println("返回的消息为:" +getMethod.getResponseBodyAsString());

getMethod.releaseConnection();

}

POST方式:

public static void postMethod() throwsException {//创建post请求,类似Post请求

PostMethod postMethod = new PostMethod( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");//设置请求的正文内容

postMethod.setRequestBody("mobileCode=18373551982&userID=");//设置传送信息的格式

postMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//发送post请求

int code =http.executeMethod(postMethod);

System.out.println("返回消息码为:" +code);

System.out.println("返回的消息为:" +postMethod.getResponseBodyAsString());

postMethod.releaseConnection();

}

2.第二种使用方式

/**HttpClient访问网络的实现步骤:

*  1. 准备一个请求客户端:浏览器

*  2. 准备请求方式: GET 、POST

*  3. 设置要传递的参数

*  4.执行请求

*  5. 获取结果

*/

get方式:(不用设置参数)

packagews_a;importjava.io.IOException;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.methods.GetMethod;importorg.junit.Test;public classHttpClientTest {

@Testpublic void testGet() throwsHttpException, Exception{

HttpClient client= newHttpClient();

GetMethod getMethod= new GetMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+"13110410513"+

"&userID="+"");//4.执行请求 ,结果码

int code=client.executeMethod(getMethod);//5. 获取结果

String result=getMethod.getResponseBodyAsString();

System.out.println("get请求的结果:"+result);

}

}

get请求的结果:<?xml version="1.0" encoding="utf-8"?>

13110410513:陕西 西安 陕西联通GSM卡

Post方法:

@Testpublic void post() throwsException{

HttpClient client=newHttpClient();

PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");//3.设置请求参数

postMethod.setParameter("mobileCode", "13834786998");

postMethod.setParameter("userID", "");//4.执行请求 ,结果码

int code=client.executeMethod(postMethod);//5. 获取结果

String result=postMethod.getResponseBodyAsString();

System.out.println("Post请求的结果:"+result);

}

Post请求的结果:<?xml version="1.0" encoding="utf-8"?>

13834786998:山西 长治 山西移动全球通卡

如果返回的中文乱码,我们可以设置编码:

//防止中文乱码

postMethod.getParams().setContentCharset("utf-8");

maven地址:

commons-httpclient

commons-httpclient

3.1

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值