怎么在java中利用HttpClient PostMethod对json数据进行提交
创始人
2024-12-29 03:02:52
0次

PostMethod请求注意两点:

1、如果使用的是公司的服务器,设置好代理和端口。

2、如果url中有空格,需要使用%20 进行转义。

贴一下我的代码 ,给不会还没用过不会PostMethod请求的萌新们…

HttpClient httpClient = new HttpClient();
 String host = (String) BaseConfig.get("host");
 String port = (String) BaseConfig.get("port");
 httpClient.getHostConfiguration().setProxy(host, Integer.valueOf(port));
 PostMethod postMethod = new PostMethod(applyurl);
 JSONObject jsonObject = new JSONObject();
 jsonObject.put("name",user.getName());
 jsonObject.put("phone",user.getPhone());
 jsonObject.put("provinceCode",user.getProvinceCode()); 
 jsonObject.put("cityCode",user.getCityCode()); 
 jsonObject.put("buyModelCode",user.getBuyModelCode()); 
 jsonObject.put("dealerCode",user.getDealerCode()); 
 jsonObject.put("url","http:xxx"); 
 String toJson = jsonObject.toString();
 RequestEntity se = new StringRequestEntity (toJson ,"application/json" ,"UTF-8");
 postMethod.setRequestEntity(se);
 postMethod.setRequestHeader("Content-Type","application/json");
 //默认的重试策略
 postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
 postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);//设置超时时间
 int httpStatus = hc.executeMethod(postMethod);
 String str=postMethod.getResponseBodyAsString();
 T.console("str-------:"+str);

代码很简单,就不过多解释了,最后感谢这个坑爹的文档,又让我学到了一招。

补充:利用HttpClient&PostMethod上传文件和请求参数

我就废话不多说了,大家还是直接看代码吧~

//HttpClient发起请求
public static String sendUrlFile(String url, String jsonstr) { 
 String result = "";
 try { 
 HttpClient httpclient = new HttpClient();
 PostMethod post = new PostMethod(url); 
 FilePart filePart = new FilePart("file", new File("/root/桌面/文档/记录.txt"));
 filePart.setCharSet("utf-8"); 
 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
 //Part数组装需要传第的参数和文件等
 Part[] parts = {new StringPart("username",jsonstr , "utf-8"),filePart};
 MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
 post.setRequestEntity(entity);
 int code = httpclient.executeMethod(post);
 //拿到响应结果
 result = new String(post.getResponseBody(), "UTF-8");
 //可释放连接
 post.releaseConnection(); 
 return result;
 } catch (HttpException h) {
 LOGGER.error("cause HttpException:" + h.getMessage()); 
 } catch (Exception i) {
 LOGGER.error("发送请求错误: url cause IOException:" + i.getMessage());
 } 
 return "";
}
//接收请求服务器端 参数需要和发送端一致
@ResponseBody
@RequestMapping(value = “/login”)
public JsonResult revice(@RequestParam(“file”) MultipartFile file,@RequestParam(“username”)String username) throws IOException{
InputStream in = file.getInputStream();
OutputStream out = new FileOutputStream("/root/桌面/ok.txt");
byte[] bs = new byte[1024];
int len;
while(-1 != (len = (in.read(bs)))){
out.write(bs);
}
JsonResult json = new JsonResult();
System.out.println();
json.setResult(“ok”);
return json;
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...