国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 利用HttpClient PostMethod提交json數(shù)據(jù)操作

瀏覽:4日期:2022-08-17 18:41:38
故事前要

今天,在做公司的一個(gè)項(xiàng)目,需要和第三方公司進(jìn)行對(duì)接,需要將我們采集到的數(shù)據(jù)發(fā)送給第三方公司,按照對(duì)方提供的文檔,傳遞好參數(shù)后,httpclient.execute(method)請(qǐng)求后,得到的狀態(tài)碼 ,一直是502,猶豫第一次使用HttpClient post json數(shù)據(jù),一直懷疑是自己的代碼問(wèn)題,最后不知在哪個(gè)技術(shù)論壇看到 ,有人問(wèn)url請(qǐng)求中有空格怎么辦,突然發(fā)現(xiàn)對(duì)方提供的pdf文檔中 竟然包含空格,而我天真的無(wú)視掉了 以為是文檔的問(wèn)題。

算了…… 不多BB了….

PostMethod請(qǐng)求注意兩點(diǎn):

1、如果使用的是公司的服務(wù)器,設(shè)置好代理和端口。

2、如果url中有空格,需要使用%20 進(jìn)行轉(zhuǎn)義。

貼一下我的代碼 ,給不會(huì)還沒(méi)用過(guò)不會(huì)PostMethod請(qǐng)求的萌新們…

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'); //默認(rèn)的重試策略 postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);//設(shè)置超時(shí)時(shí)間 int httpStatus = hc.executeMethod(postMethod); String str=postMethod.getResponseBodyAsString(); T.console('str-------:'+str);

代碼很簡(jiǎn)單,就不過(guò)多解釋了,最后感謝這個(gè)坑爹的文檔,又讓我學(xué)到了一招。

補(bǔ)充:利用HttpClient&PostMethod上傳文件和請(qǐng)求參數(shù)

我就廢話不多說(shuō)了,大家還是直接看代碼吧~

//HttpClient發(fā)起請(qǐng)求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數(shù)組裝需要傳第的參數(shù)和文件等 Part[] parts = {new StringPart('username',jsonstr , 'utf-8'),filePart}; MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams()); post.setRequestEntity(entity); int code = httpclient.executeMethod(post); //拿到響應(yīng)結(jié)果 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('發(fā)送請(qǐng)求錯(cuò)誤: url cause IOException:' + i.getMessage()); } return '';}//接收請(qǐng)求服務(wù)器端 參數(shù)需要和發(fā)送端一致

@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;}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 广丰县| 东至县| 建平县| 绩溪县| 温泉县| 东港市| 绥芬河市| 桑植县| 海伦市| 东安县| 盖州市| 湖口县| 台东市| 湛江市| 永城市| 大名县| 通渭县| 根河市| 肃南| 莎车县| 大石桥市| 湖口县| 简阳市| 乡宁县| 建水县| 大同市| 富民县| 喀什市| 安达市| 杭锦后旗| 余庆县| 潜江市| 璧山县| 锦屏县| 余干县| 北碚区| 秦皇岛市| 临湘市| 会昌县| 辰溪县| 龙南县|