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

HttpClient发送get,post接口请求的更多相关文章

  1. Android笔记---使用HttpClient发送POST和GET请求

    在Android上发送 HTTP 请求的方式一般有两种, HttpURLConnection 和 HttpClient,关于HttpURLConnection的使用方法能够參考HTTP之利用HttpU ...

  2. Java实现HttpClient发送GET、POST请求(https、http)

    1.引入相关依赖包 jar包下载:httpcore4.5.5.jar    fastjson-1.2.47.jar maven: <dependency> <groupId>o ...

  3. 使用httpclient发送get或post请求

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

  4. HttpClient 发送 HTTP、HTTPS 请求的简单封装

    import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...

  5. HttpClient发送Get和Post请求

    package JanGin.httpClient.demo; import java.io.IOException; import java.io.UnsupportedEncodingExcept ...

  6. [java,2018-01-16] HttpClient发送、接收 json 请求

    最近需要用到许多在后台发送http请求的功能,可能需要发送json和xml类型的数据. 就抽取出来写了一个帮助类: 首先判断发送的数据类型是json还是xml: import org.dom4j.Do ...

  7. 读取配置文件的URL,使用httpClient发送Post和Get请求,实现查询快递物流和智能机器人对话

    1.主要jar包: httpclient-4.3.5.jar   httpcore-4.3.2.jar 2.目录结构如图所示: 3.url.properties文件如下: geturl=http:// ...

  8. 【httpclient-4.3.1.jar】httpclient发送get、post请求以及携带数据上传文件

    1.发送get.post携带参数以及post请求接受JSON数据: package cn.qlq.utils; import java.io.BufferedReader; import java.i ...

  9. java使用HttpClient 发送get、pot请求

    package eidolon.messageback.PostUtil; import java.io.BufferedReader; import java.io.IOException; imp ...

随机推荐

  1. swift--控件工厂类的实现

    控件工厂类,简而言之就是,减少代码的复用率,只在哪里用,然后在哪里调: 代码如下: import UIKit class ViewFactory: UIView,UITextFieldDelegate ...

  2. Windows版Nginx启动失败之1113: No mapping for the Unicode character exists in the target multi-byte code page

    Windows版Nginx启动一闪,进程中未发现nginx进程,查看nginx日志,提示错误为1113: No mapping for the Unicode character exists in ...

  3. imx6ul开发板

    Feescale飞思卡尔于发布全新的基于ARM Cortex-A7核心的低功耗处理器i.MX6UL,主要面向车载信息处理.家庭能源管理系统.工控领域.物联网网关等应用.具有可扩展性.高性能和低功耗特性 ...

  4. Redhat系列使用ISO或者光盘制作yum本地安装源

    适用系统:redhat系列 示例系统:CentOS 6x86_64 环境:虚拟机   有时候做实验又不能联网的情况下想装一款软件时,经常会搞的头大:难道要去系统镜像中一个个拷出来再用rpm安装?各种依 ...

  5. 新版本读取老版本文件崩溃BUG

    读取文件匹配代码 BOOL CWBPage::LoadFromFile(CFile *pFile, LONGLONG& lOff, ULONGLONG lFileLength) { if (p ...

  6. 【Mysql】大数据处理优化方法

    1.应尽量避免在 where 子句中使用 != 或  <>  操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 orde ...

  7. Excel 2010 Alt+; (分号) --- “只选定当前选定区域中的可视单元格”

    excel怎样把筛选出来的加上颜色? 1.选中筛选结果数据区域: 2.同时按下Alt+; (分号)键,选中筛选出的数据: 3.鼠标右键,设置单元格格式: 4.在弹出的对话框中,设置字体.背景颜色,即可 ...

  8. Excel打开csv文件乱码问题的解决办法

    excel打开csv 出现乱码怎么解决 https://jingyan.baidu.com/article/ac6a9a5e4c681b2b653eacf1.html CSV是逗号分隔值的英文缩写,通 ...

  9. 360全景图three.js与Photo-Sphere-Viewer-master 3D全景浏览开发

    1.支持WebGL和canvas的浏览器 (IE10, IE11支持, 但在IE里移动图片时很卡, 不一定是全部人都有这情况) 2.Three.js (文件较大, 有官网demo, 可不下载, 下载p ...

  10. Windows Phone 7 程序等待页面的处理

    程序启动通常会有一个等待的过程,在这个过程中可以通过使用Popup控件配合BackgroundWorker类启动后台线程来实现. 控件的代码 PopupSplash.xaml <UserCont ...