HttpClient发送get,post接口请求
2024-08-29 20:05:22
HttpClient发送get post接口请求/* * post
* @param url POST地址
* @param data POST数据NameValuePair[]
* @return 响应的参数
*/
public static String post(String url,NameValuePair[] data){---------------get里面没有data只有url String response = ""; HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url); //设置请求编码
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK"); postMethod.setRequestBody(data); ---------------------------------get没有这一步-------- //设置请求头header参数
postMethod.setRequestHeader("userId","57601095");
postMethod.setRequestHeader("userSession","57601095");
postMethod.setRequestHeader("cityid","31000");
int statusCode = 0;
JSONObject jsonResult = null; try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
byte[] b=postMethod.getResponseBody();
response = new String(b,"GBK");
jsonResult = JSONObject.fromObject(response); //将返回结果变成json格式
} catch (IOException e) {
e.printStackTrace();
}
postMethod.releaseConnection();
System.out.println("POST响应参数:"+response);
return response;
}
下面是带有301重定向的完整代码
/**
* post
* @param url POST地址
* @param data POST数据NameValuePair[]
* @return 响应的参数
*/
public static String post(String url,NameValuePair[] data){------------------------get里面没有data只有url String response = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url); //设置请求编码
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK");
postMethod.setRequestBody(data);--------------------------------get没有这一步 //设置请求头header参数
postMethod.setRequestHeader("userId","57601095");
postMethod.setRequestHeader("userSession","57601095");
postMethod.setRequestHeader("cityid","31000"); int statusCode = 0; try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} //301、302重定向
if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("重定向跳转页面:" + location);
response= post(location,data);//用跳转后的页面重新请求。
} else {
System.err.println("location is null.");
}
}else {
System.out.println(postMethod.getStatusLine());
try {
byte[] b=postMethod.getResponseBody();
response = new String(b,"GBK");
} catch (IOException e) {
e.printStackTrace();
}
postMethod.releaseConnection();
} System.out.println("POST响应参数:"+response); return response;
}
最新文章
- Socket通信(二)
- GD库处理图像
- 据说年薪30万的Android程序员必须知道的帖子
- 什么是hasLayout?
- 1003. Emergency
- MC java 远程调试 plugin 开发
- ImageSource的使用
- How to use Ubuntu Linux in virtual box
- css3绘制中国结
- RPATH与RUNPATH
- rownum使用方法
- Java分布式应用技术架构
- access登录校验代码二
- 使用数组制作简易的用户管理系统【java】
- wstngfw openVpn站点到站点连接示例(共享密钥)
- springbank 开发日志 SpringMVC是如何找到handler找到对应的方法并执行的
- Shell日期处理
- 什么是Java序列化?为什么序列化?序列化有哪些方式?
- java 实现 PDF 加水印功能
- Unity3D 程序打包报错(程序是连接数据库进行处理的)
热门文章
- [转]ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
- 文件打包为zip格式文件下载
- Excel时间格式修改为文本格式
- 超全面的JavaWeb笔记day19<;Service>;
- 给IT同学推荐这15个不错的学习网站,收藏起来慢慢看吧
- IIS7以上版本去掉伪静态去掉index.php方法
- C++异常 将对象用作异常类型
- 《C++ Primer Plus》10.2 抽象和类 学习笔记
- linux配置免密登录
- poj_2396 有上下界的网络流