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;
}

最新文章

  1. Socket通信(二)
  2. GD库处理图像
  3. 据说年薪30万的Android程序员必须知道的帖子
  4. 什么是hasLayout?
  5. 1003. Emergency
  6. MC java 远程调试 plugin 开发
  7. ImageSource的使用
  8. How to use Ubuntu Linux in virtual box
  9. css3绘制中国结
  10. RPATH与RUNPATH
  11. rownum使用方法
  12. Java分布式应用技术架构
  13. access登录校验代码二
  14. 使用数组制作简易的用户管理系统【java】
  15. wstngfw openVpn站点到站点连接示例(共享密钥)
  16. springbank 开发日志 SpringMVC是如何找到handler找到对应的方法并执行的
  17. Shell日期处理
  18. 什么是Java序列化?为什么序列化?序列化有哪些方式?
  19. java 实现 PDF 加水印功能
  20. Unity3D 程序打包报错(程序是连接数据库进行处理的)

热门文章

  1. [转]ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
  2. 文件打包为zip格式文件下载
  3. Excel时间格式修改为文本格式
  4. 超全面的JavaWeb笔记day19<Service>
  5. 给IT同学推荐这15个不错的学习网站,收藏起来慢慢看吧
  6. IIS7以上版本去掉伪静态去掉index.php方法
  7. C++异常 将对象用作异常类型
  8. 《C++ Primer Plus》10.2 抽象和类 学习笔记
  9. linux配置免密登录
  10. poj_2396 有上下界的网络流