【commons-httpclient】Java中HttpClient工具访问Web请求
注意jar包是:
HttpClient工具使用
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
为什么要使用HttpClient工具:
原生态的Socket基于传输层,现在我们要访问的WebService是基于HTTP的属于应用层,所以我们的Socket通信要借助HttpClient发HTTP请求,这样格式才能匹配
HttpClient使用步骤如下:
- 创建 HttpClient 的实例
- 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
- 配置要传输的参数,和消息头信息
- 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
- 通过response读取字符串
- 释放连接。无论执行方法是否成功,都必须释放连接
jar包:
1。第一种使用方式:
Get方式:
public static void getMethod() throws Exception { // 创建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() throws Exception { // 创建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方式:(不用设置参数)
package ws_a; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; import org.junit.Test; public class HttpClientTest { @Test public void testGet() throws HttpException, Exception{ HttpClient client = new HttpClient(); 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"?>
<string xmlns="http://WebXml.com.cn/">13110410513:陕西 西安 陕西联通GSM卡</string>
Post方法:
@Test public void post() throws Exception{ HttpClient client=new HttpClient(); 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"?>
<string xmlns="http://WebXml.com.cn/">13834786998:山西 长治 山西移动全球通卡</string>
如果返回的中文乱码,我们可以设置编码:
// 防止中文乱码 postMethod.getParams().setContentCharset("utf-8");
maven地址:
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了