java模拟get/post请求
2016-01-26 21:51
471 查看
一、背景介绍
1、有时本系统和第三方系统交互时会涉及get和post类型的请求。
二、实现逻辑
1、GET方式的请求和浏览器中差不多,都要把参数附加到url后边。主要用的是getMethod封装和httpClient调用的exacuteMethod方法执行getMethod。
2、POST方式和GET方式不同,它主要是使用NameValuePair数组结构将参数对象封装,然后用PostMethod封装url路径,其他的都相似。
三、具体实现
1、GET请求
String responseMsg = "";
// 1.构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 用于测试的http接口的url
String url="http://183.129.138.123:8080/mobile/mobile/open/fund/records?param1="+param1+"¶m2="+param2;
// 2.创建GetMethod的实例
GetMethod getMethod = new GetMethod(url);
// 使用系统系统的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//3.执行getMethod,调用http接口
httpClient.executeMethod(getMethod);
//4.读取内容
byte[] responseBody = getMethod.getResponseBody();
//5.处理返回的内容
responseMsg = new String(responseBody);
log.info(responseMsg);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//6.释放连接
getMethod.releaseConnection();
}
return responseMsg;
2、POST请求
String responseMsg = "";
//1.构造HttpClient的实例
HttpClient httpClient=new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");
String url = URL + path;
//2.构造PostMethod的实例
PostMethod postMethod=new PostMethod(url);
//3.把参数值放入到PostMethod对象中
//方式1:
/*NameValuePair[] data = { new NameValuePair("param1", param1),
new NameValuePair("param2", param2) };*/
postMethod.setRequestBody(data);
//方式2:
//postMethod.addParameter("param1", param1);
//postMethod.addParameter("param2", param2);
try {
// 4.执行postMethod,调用http接口
httpClient.executeMethod(postMethod);//200
//5.读取内容
responseMsg = postMethod.getResponseBodyAsString();
log.info(responseMsg);
//6.处理返回的内容
} catch (Exception e) {
throw e;
} finally {
//7.释放连接
postMethod.releaseConnection();
}
return responseMsg;
}
3、POST的测试方法
/**
* 测试的main方法
* @param args
*/
public static void main(String[] args) {
/* NameValuePair[] data = new NameValuePair[6];
String path = ""; //设置预约平台访问路径
data[0] = new NameValuePair("scheduleDate", "20160118");
data[1] = new NameValuePair("timeSegStr", "");
data[2] = new NameValuePair("typeStr", "");
data[3] = new NameValuePair("deptId", "0");
data[4] = new NameValuePair("doctorId", "0");
data[5] = new NameValuePair("partnerToken", "69A4EC1F153141AECF10845E8E841142");
path = "/schedule/searchSchedule";
String responseJson = postHttp(data, path);
System.out.println("post方式调用http接口\n" + responseJson); */
NameValuePair[] data = new NameValuePair[3];
String path = ""; //设置预约平台访问路径
//data[0] = new NameValuePair("token", "4FE03VV7ACP9128CEP9C09XIAMW710SV09F3CVSF8CE7Z0B892BSYA0XFL1Z04A");
data[0] = new NameValuePair("hospitalId", "10266");
data[1] = new NameValuePair("startDate", "2015-11-01 12:00:00");
data[2] = new NameValuePair("endDate", "2016-01-22 12:00:00");
path = "mobile-web/open/trade/fund/record";
String responseJson;
try {
responseJson = postHttp(data, path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4、需要的JAR的maven配置
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
1、有时本系统和第三方系统交互时会涉及get和post类型的请求。
二、实现逻辑
1、GET方式的请求和浏览器中差不多,都要把参数附加到url后边。主要用的是getMethod封装和httpClient调用的exacuteMethod方法执行getMethod。
2、POST方式和GET方式不同,它主要是使用NameValuePair数组结构将参数对象封装,然后用PostMethod封装url路径,其他的都相似。
三、具体实现
1、GET请求
String responseMsg = "";
// 1.构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 用于测试的http接口的url
String url="http://183.129.138.123:8080/mobile/mobile/open/fund/records?param1="+param1+"¶m2="+param2;
// 2.创建GetMethod的实例
GetMethod getMethod = new GetMethod(url);
// 使用系统系统的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//3.执行getMethod,调用http接口
httpClient.executeMethod(getMethod);
//4.读取内容
byte[] responseBody = getMethod.getResponseBody();
//5.处理返回的内容
responseMsg = new String(responseBody);
log.info(responseMsg);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//6.释放连接
getMethod.releaseConnection();
}
return responseMsg;
2、POST请求
String responseMsg = "";
//1.构造HttpClient的实例
HttpClient httpClient=new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");
String url = URL + path;
//2.构造PostMethod的实例
PostMethod postMethod=new PostMethod(url);
//3.把参数值放入到PostMethod对象中
//方式1:
/*NameValuePair[] data = { new NameValuePair("param1", param1),
new NameValuePair("param2", param2) };*/
postMethod.setRequestBody(data);
//方式2:
//postMethod.addParameter("param1", param1);
//postMethod.addParameter("param2", param2);
try {
// 4.执行postMethod,调用http接口
httpClient.executeMethod(postMethod);//200
//5.读取内容
responseMsg = postMethod.getResponseBodyAsString();
log.info(responseMsg);
//6.处理返回的内容
} catch (Exception e) {
throw e;
} finally {
//7.释放连接
postMethod.releaseConnection();
}
return responseMsg;
}
3、POST的测试方法
/**
* 测试的main方法
* @param args
*/
public static void main(String[] args) {
/* NameValuePair[] data = new NameValuePair[6];
String path = ""; //设置预约平台访问路径
data[0] = new NameValuePair("scheduleDate", "20160118");
data[1] = new NameValuePair("timeSegStr", "");
data[2] = new NameValuePair("typeStr", "");
data[3] = new NameValuePair("deptId", "0");
data[4] = new NameValuePair("doctorId", "0");
data[5] = new NameValuePair("partnerToken", "69A4EC1F153141AECF10845E8E841142");
path = "/schedule/searchSchedule";
String responseJson = postHttp(data, path);
System.out.println("post方式调用http接口\n" + responseJson); */
NameValuePair[] data = new NameValuePair[3];
String path = ""; //设置预约平台访问路径
//data[0] = new NameValuePair("token", "4FE03VV7ACP9128CEP9C09XIAMW710SV09F3CVSF8CE7Z0B892BSYA0XFL1Z04A");
data[0] = new NameValuePair("hospitalId", "10266");
data[1] = new NameValuePair("startDate", "2015-11-01 12:00:00");
data[2] = new NameValuePair("endDate", "2016-01-22 12:00:00");
path = "mobile-web/open/trade/fund/record";
String responseJson;
try {
responseJson = postHttp(data, path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4、需要的JAR的maven配置
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树