介绍 PostMethod请求注意两点:
这期内容当中小编将会给大家带来有关怎么在java中利用HttpClient PostMethod对json数据进行提交,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
PostMethod请求注意两点:
1,如果使用的是公司的服务器,设置好代理和端口。
2,如果url中有空格,需要使用% 20进行转义。
贴一下我的代码,给不会还没用过不会PostMethod请求的萌新们…
HttpClient HttpClient =, new HttpClient (); ,String host =,(字符串),BaseConfig.get (“host"); ,String port =,(字符串),BaseConfig.get (“port"); ,httpClient.getHostConfiguration () .setProxy(主机,Integer.valueOf(港口)); ,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 ,“应用程序/json",,“UTF-8"); ,postMethod.setRequestEntity (se); ,postMethod.setRequestHeader (“Content-Type",“应用程序/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 文件(“/根/桌面/文档/记录.txt")); ,filePart.setCharSet (“utf-8");, ,post.getParams () .setParameter (HttpMethodParams.HTTP_CONTENT_CHARSET,“utf-8"); ,//数部分组装需要传第的参数和文件等 ,部分[]parts =, {new StringPart (“username" jsonstr ,,“utf-8"), filePart}; ,MultipartRequestEntity entity =, new MultipartRequestEntity(部分,post.getParams ()); ,post.setRequestEntity(实体); ,int code =, httpclient.executeMethod (post); ,//拿到响应结果=,,result  new 字符串(post.getResponseBody (),“UTF-8"); ,//可释放连接 ,post.releaseConnection (),, ,return 结果; ,}catch (HttpException h), { ,LOGGER.error (“cause  textbox:“, +, h.getMessage ()),, ,}catch (Exception i), { ,LOGGER.error(“发送请求错误:,url cause IOException:“, +, i.getMessage ()); }大敌; ,return ““; }//接收请求服务器端,参数需要和发送端一致
@ResponseBody @RequestMapping (value =,/登录) public JsonResult  revice (@RequestParam(“文件”),MultipartFile 文件,@RequestParam(“用户名”)String 用户名),throws IOException { InputStream 拷贝=,file.getInputStream (); OutputStream out =, new FileOutputStream(“/根/桌面/ok.txt"); 时间=byte [], bs new 字节[1024]; int 兰; 而(1,!=,(len =, (in.read (bs)))) { out.write (bs); } JsonResult json =, new JsonResult (); System.out.println (); json.setResult (“ok”); return json; }
上述就是小编为大家分享的怎么在java中利用HttpClient PostMethod对json数据进行提交了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。