httpclient的使用
2011-06-27 09:04
387 查看
一.httpclient客户端:
@Test
public void testSendMsg() throws Exception {
HttpClient client = new HttpClient();
//设置主机名和端口名
client.getHostConfiguration().setHost("http://localhost:8080/", 8080);
//设置post的方法
PostMethod post = new PostMethod("http://localhost:8080/rili_service/inner/SendMsg.action");
//防止中文参数的乱码
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//设置post过去的参数
post.addParameter(new NameValuePair("sourceTermMDN","1182026"));
post.addParameter(new NameValuePair("destTermMDN", "18910895580;18951732225"));
post.addParameter(new NameValuePair("messageContent",new String("收到的中文,有乱码吗?".getBytes("utf-8"))));
post.addParameter(new NameValuePair("userId", "99016768226"));
post.addParameter(new NameValuePair("sendIp", ""));
post.addParameter(new NameValuePair("sendType", "1"));
client.executeMethod(post);
//取返回码
String responseString = post.getResponseBodyAsString();
System.out.println("responseString="+responseString);
//取返回页面的状态码,为200表示正常返回;
System.out.println("状态码="+post.getStatusCode());
System.out.println("开始post.........");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
}
二.接收httpclient 传过来的参数
public String SendMsg()
{
System.out.println("开始SendMsg......");
int result=0;
//接收post过来的参数
String sourceTermMDN=getStringValue("sourceTermMDN");
String[] destTermMDN=getStringValue("destTermMDN").split(";");
String messageContent=getStringValue("messageContent");
String userId=getStringValue("userId");
String sendIp=getStringValue("sendIp");
String sendType=getStringValue("sendType");
//调用的业务处理函数
result=smsService.sendSysSms(id, sourceTermMDN, destTermMDN, messageContent, userId, sendIp, sendType);
System.out.println("结束SendMsg......");
//输出回应页面
doOutput(String.valueOf(result));
return null;
}
/**
* 获得传递的参数值
* @param key 参数名称
* @return 参数值
*/
public String getStringValue(String key) {
try {
return ((String[]) ActionContext.getContext().getParameters().get(key))[0];
}
catch(Exception ex) {
return "";
}
}
public void doOutput(String content) {
HttpServletResponse response = getResponse();
response.setCharacterEncoding("UTF-8");
try {
OutputStream os = response.getOutputStream();
os.write(content.getBytes("UTF-8"));
os.flush();
os.close();
if(logger.isDebugEnabled()){
logger.debug("response content:" + content);
}
} catch (IOException e) {
logger.error(e);
}
}
@Test
public void testSendMsg() throws Exception {
HttpClient client = new HttpClient();
//设置主机名和端口名
client.getHostConfiguration().setHost("http://localhost:8080/", 8080);
//设置post的方法
PostMethod post = new PostMethod("http://localhost:8080/rili_service/inner/SendMsg.action");
//防止中文参数的乱码
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//设置post过去的参数
post.addParameter(new NameValuePair("sourceTermMDN","1182026"));
post.addParameter(new NameValuePair("destTermMDN", "18910895580;18951732225"));
post.addParameter(new NameValuePair("messageContent",new String("收到的中文,有乱码吗?".getBytes("utf-8"))));
post.addParameter(new NameValuePair("userId", "99016768226"));
post.addParameter(new NameValuePair("sendIp", ""));
post.addParameter(new NameValuePair("sendType", "1"));
client.executeMethod(post);
//取返回码
String responseString = post.getResponseBodyAsString();
System.out.println("responseString="+responseString);
//取返回页面的状态码,为200表示正常返回;
System.out.println("状态码="+post.getStatusCode());
System.out.println("开始post.........");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
}
二.接收httpclient 传过来的参数
public String SendMsg()
{
System.out.println("开始SendMsg......");
int result=0;
//接收post过来的参数
String sourceTermMDN=getStringValue("sourceTermMDN");
String[] destTermMDN=getStringValue("destTermMDN").split(";");
String messageContent=getStringValue("messageContent");
String userId=getStringValue("userId");
String sendIp=getStringValue("sendIp");
String sendType=getStringValue("sendType");
//调用的业务处理函数
result=smsService.sendSysSms(id, sourceTermMDN, destTermMDN, messageContent, userId, sendIp, sendType);
System.out.println("结束SendMsg......");
//输出回应页面
doOutput(String.valueOf(result));
return null;
}
/**
* 获得传递的参数值
* @param key 参数名称
* @return 参数值
*/
public String getStringValue(String key) {
try {
return ((String[]) ActionContext.getContext().getParameters().get(key))[0];
}
catch(Exception ex) {
return "";
}
}
public void doOutput(String content) {
HttpServletResponse response = getResponse();
response.setCharacterEncoding("UTF-8");
try {
OutputStream os = response.getOutputStream();
os.write(content.getBytes("UTF-8"));
os.flush();
os.close();
if(logger.isDebugEnabled()){
logger.debug("response content:" + content);
}
} catch (IOException e) {
logger.error(e);
}
}
相关文章推荐
- 使用单个httpclient实例请求数据。
- 接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
- Post和Get在HttpClient的使用
- HttpClient_使用httpclient必须知道的参数设置及代码写法、存在的风险
- 使用HttpClient对ASP.NET Web API服务实现增删改查
- Cocos2d-x3.0 如何使用HttpClient
- .NET Core中使用HttpClient的正确姿势
- 使用Apache HttpClient实现多线程下载的小例子
- 使用HttpClient请求数据显示在ListView上
- apache httpclient和fileupload包使用
- 使用httpClient连接https时使用服务端证书(self-signed certificate)的方法
- Android网络请求---AsyncHttpClient的使用
- HttpClient4.5 使用http连接池发送http请求深度示例
- android中使用HttpClient实现长久登陆
- 使用HttpClient 4.3.4 自动登录并抓取中国联通用户基本信息和账单数据,GET/POST/Cookie
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- 使用httpclient实现上传下载(javaWeb系统数据传输http实现)
- HttpClient 使用详解
- 使用httpclient post请求中文乱码解决办法
- 使用HttpClient实现接口测试