亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

httpclient調接口post出現無響應400

httpclient調接口post出現無響應400

qq_fly_5 2020-01-07 15:17:13
???String?uri=url+bundle.getString("postCookie.uri"); ????//生成httpclient對象 ???//?CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build(); ???DefaultHttpClient?httpClient?=?new?DefaultHttpClient(); ????//創建post請求 ????HttpPost?httpPost=new?HttpPost(uri); ????//使用json入參,入參是中文時不行。 ????JSONObject?param=new?JSONObject(); ????param.put("name","qinzhenxia"); ????param.put("age","28"); ????//設置請求頭header ????httpPost.setHeader("Content-Type","application/json;charset=gbk"); ????//傳入參數 ????StringEntity?entity=new?StringEntity(param.toString(),"gbk"); ????httpPost.setEntity(entity); ????//聲明一個client對象,用來進行方法的執行,并設置cookies信息 ????CloseableHttpClient?httpclient?=?HttpClients.custom().setDefaultCookieStore(this.store).build(); ????//執行請求并接收返回結果 ????HttpResponse?httpResponse?=?httpClient.execute(httpPost); ????String?result=EntityUtils.toString(httpResponse.getEntity()); ????//判斷返回code值 ????int?statuscode=httpResponse.getStatusLine().getStatusCode(); ????if?(statuscode==200){ ????????System.out.println(result); ????}?else?{ ????????System.out.println("訪問/postwithcookiesandjsontwo接口失敗"); ????} ????//獲取返回結果并轉換為jsonobject格式,然后獲取某個具體的值通過斷言判斷 ????JSONObject?resultjson=new?JSONObject(result); ????String?msg?=resultjson.getString("msg"); ????//判斷msg等于success ????Assert.assertEquals(msg,"success"); ????JSONObject?p2pdata=resultjson.getJSONObject("p2pdata"); ????String?address=p2pdata.getString("address"); ????Assert.assertEquals(address,"北三環"); ????System.out.println(address); }訪問/postwithcookiesandjsontwo接口失敗
查看完整描述

2 回答

?
慕移動7568944

TA貢獻1條經驗 獲得超0個贊

//DefaultHttpClient httpClient = new DefaultHttpClient();
上面這句注釋掉

HttpResponse httpResponse = httpClient.execute(httpPost);
httpClient改成httpclient,即
HttpResponse httpResponse = httpclient.execute(httpPost);

?

查看完整回答
反對 回復 2021-02-03
?
安浪創想

TA貢獻81條經驗 獲得超23個贊

你用http 測試工具訪問正常不正常再檢查代碼。也就是服務端接口正常了,才能說明你的客戶端代碼有問題。

查看完整回答
反對 回復 2020-01-28
  • 2 回答
  • 0 關注
  • 1771 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號