HttpClient入门实例
2016-04-12 09:03
681 查看
Struts2的Action配置代码
Java代码
<package name="Ajax" extends="json-default" namespace="/Ajax">
<action name="serivceJ" class="com.wanghe.test.TestAction" method="serivceJ" >
<result type="json"></result>
</action>
</package>
Action代码
Java代码
public void serivceJ() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("request...serivceJ");
response.setCharacterEncoding("UTF-8");
String type = request.getParameter("type");
String c = "none";
if(type.equalsIgnoreCase("girl")){
c = "女孩你好!";
}else if(type.equalsIgnoreCase("boy")){
c = "男孩你好!";
}
response.getWriter().write(c);
} catch (IOException e) {
e.printStackTrace();
}
}
HttpClient测试代码
Java代码
//创建默认的httpClient实例.
HttpClient httpclient = new DefaultHttpClient();
//创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
//创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("type", "girl"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) { System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8"));
System.out.println("--------------------------------------");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//关闭连接,释放资源
httpclient.getConnectionManager().shutdown();
}
输出:
Java代码
executing request http://localhost:8080/myDemo/Ajax/serivceJ.action --------------------------------------
Response content: 女孩你好!
--------------------------------------
Java代码
<package name="Ajax" extends="json-default" namespace="/Ajax">
<action name="serivceJ" class="com.wanghe.test.TestAction" method="serivceJ" >
<result type="json"></result>
</action>
</package>
Action代码
Java代码
public void serivceJ() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("request...serivceJ");
response.setCharacterEncoding("UTF-8");
String type = request.getParameter("type");
String c = "none";
if(type.equalsIgnoreCase("girl")){
c = "女孩你好!";
}else if(type.equalsIgnoreCase("boy")){
c = "男孩你好!";
}
response.getWriter().write(c);
} catch (IOException e) {
e.printStackTrace();
}
}
HttpClient测试代码
Java代码
//创建默认的httpClient实例.
HttpClient httpclient = new DefaultHttpClient();
//创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
//创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("type", "girl"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) { System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8"));
System.out.println("--------------------------------------");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//关闭连接,释放资源
httpclient.getConnectionManager().shutdown();
}
输出:
Java代码
executing request http://localhost:8080/myDemo/Ajax/serivceJ.action --------------------------------------
Response content: 女孩你好!
--------------------------------------
相关文章推荐
- Https 原理
- Bzoj2229:[Zjoi2011]最小割:分治最小割
- HttpApplication处理对象与HttpModule处理模块
- HttpHandler与HttpModule的用处与区别
- http协议特点
- 网络数据包分析
- Linux网络基础知识
- 一个计算机网络的面试题
- 基于Java的tcp实现文本聊天系统
- 网络编程学习——I/O复用(一)
- http://www.y-liang.cn/post-50.html
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- ubunto apt config proxy 配置代理或者运行在代理网络的环境下
- HTTP协议之状态码
- 【ios开发学习】【swift】Xcode7 使用NSURLSession发送HTTP请求的问题
- 计算机网络-抓包
- 3115 高精度练习之减法——http://codevs.cn/problem/3115/
- ASP.NET IHttpAsyncHandler的一点想法
- HTTPS详解
- 广东工业大学2016校赛决赛-网络赛 D E F G 题解