您的位置:首页 > 理论基础 > 计算机网络

commons-httpclient简单使用

2014-02-06 13:12 323 查看
使用jar包gson-2.2.4.jar                commons-httpclient-3.1_1.jar commons-http-1.1.jar

package com.ucs.tdc.controller;
import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.annotation.Resource;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.PostMethod;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.ucs.tdc.interFace.TestInterFace;import com.ucs.tdc.pojo.AdminInfo;
@Controllerpublic class TestController { @Resource private TestInterFace interFace; public void setInterFace(TestInterFace interFace) { this.interFace = interFace; }

测试请求地址 http://localhost:8080/UCS_TDC/jsp/user/do_adduser.do?name=zhang&&password=123456 /**保存信息 * @throws IOException  * @throws HttpException ***/ @RequestMapping(value="jsp/user/do_adduser") @ResponseBody public boolean do_adduser(@RequestParam String name,@RequestParam String password) throws HttpException, IOException { boolean b=false;//是否保存成功 List<AdminInfo> list=new ArrayList<AdminInfo>(); AdminInfo info=new AdminInfo(); info.setId(1); info.setUserName(name); info.setUserPw(password); list.add(info); Gson gson=new Gson(); String str=gson.toJson(list); /**httpclient***/ HttpClient client = new HttpClient( );  String url = "http://192.168.2.99:8080/UCS_TDC/jsp/user/do_test.do?"; PostMethod method = new UTF8PostMethod(url); method.setParameter("valueinfo", str); client.executeMethod(method);  //打印服务器返回的状态          System.out.println(method.getStatusLine());          //打印返回的信息          System.out.println("接收到服务器返回的字符串"+method.getResponseBodyAsString());                  String str2=method.getResponseBodyAsString();        /***将从服务器收到的字符串进行gson转化*/       List<AdminInfo>  adlist=gson.fromJson(str2,                 new TypeToken<List<AdminInfo>>() {         }.getType());          System.out.println("经过修改的对象  账号:"+adlist.get(0).getUserName()+"   密码:"+adlist.get(0).getUserPw());        //释放连接          method.releaseConnection();   return b; } @RequestMapping(value="jsp/user/do_test") @ResponseBody public List<AdminInfo>  do_test(@RequestParam String valueinfo) { System.out.println("===================="+valueinfo+"=================================="); Gson gson=new Gson(); List<AdminInfo> infolist=gson.fromJson(valueinfo,                  new TypeToken<List<AdminInfo>>() {          }.getType());   AdminInfo info=infolist.get(0); System.out.println("000000"+info.getUserName()); return infolist; }
/***重写编码设置   httpclient发送的url默认是  ISO-8859-1    我们的整的项目使用编码为utf-8 所以需要重写他的方法,修改编码格式**/ public static class UTF8PostMethod extends PostMethod{        public UTF8PostMethod(String url){            super(url);        }        @Override        public String getRequestCharSet() {            //return super.getRequestCharSet();            return "UTF-8";        }    }}
 method.setParameter("valueinfo", str);是用来设置需要穿的参数参数为String  但是我们看出,我们如果需要保存大量数据的时候,我们将这下数据转为为对象,将对象用gson转化为String然后添加到要传的参数中,这样我们就很方便传输一个对象,只需要保证两个服务的属性一样。就能用gson重新将字符转为对象。传递少量参数时,我们就直接将参数加上去。从服务器返回的数据: String str2=method.getResponseBodyAsString();
释放数据:method.releaseConnection();  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: