java + httpclient +post请求(记录下)
生活随笔
收集整理的這篇文章主要介紹了
java + httpclient +post请求(记录下)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
public void testMethodpost(){
//初始化http請求
CloseableHttpClient httpclient=HttpClientBuilder.create().build();
String url = "xxxxxxxxxxxxx";
//初始化http請求
HttpPost post=new HttpPost(url);
//設置請求頭
post.setHeader("Content-Type","application/json;charset=UTF-8");
try {
// JSONObject jsonObject = JSON.parseObject(paramJson);
//
// System.out.println(jsonObject);
// String param=JSON.toJSONString(jsonObject);
// 提交參數(shù)發(fā)送請求StringEntity requestEntity = new StringEntity(paramJson,"UTF-8");post.setEntity(requestEntity);CloseableHttpResponse response=httpclient.execute(post);HttpEntity entity=response.getEntity();String entityStr =EntityUtils.toString(entity,"UTF-8");System.out.println(entityStr);//httpclient.close();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} }遇到的坑: 1.前期使用POST數(shù)據(jù)時,用了UrlEncodedFormEntity()這個方法 結果服務器返回500,打印的請求參數(shù)為:key=value&key2=value2格式解決辦法:使用 StringEntity requestEntity = new StringEntity(paramJson,"UTF-8");辦法解決轉載于:https://blog.51cto.com/12390959/2372182
總結
以上是生活随笔為你收集整理的java + httpclient +post请求(记录下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人生第一个过 5K Star 的 项目
- 下一篇: CI持续集成系统环境---部署Gitla