挺好用的一个Http请求发送类,给我自己做一个笔记吧~
1. 添加maven包
1.1 pom文件直接添加
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
1.2 libs下新增jar包
下载地址:https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient/3.0.1
代码块:
public Result<?> rcLogin(HttpServletRequest request){
String url = "http://xxxxxxxxxxxx";
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");
GetMethod getMethod = new GetMethod(url);
//PostMethod postMethod = new PostMethod(url);
//请求成功code
int lineStatus = httpClient.executeMethod(getMethod);
//请求返回体,这个方法是JsonString,需要自己转一下
String responseBodyAsString = getMethod.getResponseBodyAsString();
//然后下面就做自己的操作了.....
}
文章介绍了如何在Java中使用HttpClient库进行HTTP请求。首先,通过在Maven项目的pom.xml文件中添加commons-httpclient依赖,或者直接下载jar包到libs目录。接着,展示了创建HttpClient对象,设置字符集,执行GET请求并获取响应体的方法。返回的响应体通常需要进一步处理,例如转换为JSON格式。
1196

被折叠的 条评论
为什么被折叠?



