时间:2021-05-19
今天,在做公司的一个项目,需要和第三方公司进行对接,需要将我们采集到的数据发送给第三方公司,按照对方提供的文档,传递好参数后,httpclient.execute(method)请求后,得到的状态码 ,一直是502,犹豫第一次使用HttpClient post json数据,一直怀疑是自己的代码问题,最后不知在哪个技术论坛看到 ,有人问url请求中有空格怎么办,突然发现对方提供的pdf文档中 竟然包含空格,而我天真的无视掉了 以为是文档的问题。
算了…… 不多BB了….
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;}以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了phparray转json及java转换json数据格式操作。分享给大家供大家参考,具体如下:phparray转json数据$arr=array(
本文实例讲述了PHP基于curl模拟post提交json数据。分享给大家供大家参考,具体如下:这里php模拟post提交json数据操作的关键是在头部设置Con
本文实例讲述了Java实现操作JSON的便捷工具类。分享给大家供大家参考,具体如下:对于JSON数据格式的处理,自开发Java以来,已用过多种JSON的开源工具
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请
HttpClient使用post方法提交数据源代码:复制代码代码如下:packagepost;importJava.io.IOException;importo