這期內容當中小編將會給大家帶來有關怎么在java中利用HttpClient PostMethod對json數據進行提交,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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;
}上述就是小編為大家分享的怎么在java中利用HttpClient PostMethod對json數據進行提交了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。