使用Java HttpComponent/HttpClinet 調用 WebAPI問題的解決

來源:互聯網
上載者:User

標籤:

幾個關鍵字:

WebAPI, Android, Apache HttpComponent/HttpClient

問題:無論怎麼測試, WebAPI的FormBody value總是空.

 

最簡單的代碼 WebAPI, 沒有加認證

 1 // POST api/<controller> 2 [HttpPost] 3 public string Post([FromBody]string value) 4 { 5     string val = value; 6  7     Debug.WriteLine(value); 8  9     return value;10 }

 

最簡單的代碼, Java端,用StringEntity

 1     public static void NotAuthTestPost() throws IOException { 2         CloseableHttpClient httpclient = HttpClients.createDefault(); 3         try { 4             HttpPost hp = new HttpPost("http://localhost:8888/api/test"); 5              6             String str="{‘Header‘:[{‘summary‘:111,‘endtime‘:570,‘end_date‘:1431381600,‘resid‘:‘sc20150512034635‘,‘starttime‘:570,‘scheduleid‘:‘sc155485e5025f3d‘,‘machid‘:‘sc155485e8044b93‘,‘parentid‘:null,‘start_date‘:1431381600}],‘Detail‘:[{‘perm_modify‘:1,‘owner‘:1,‘perm_delete‘:1,‘resid‘:‘sc20150512034635‘,‘memberid‘:‘sc1555070e4d8217‘,‘invited‘:0},{‘perm_modify‘:0,‘owner‘:0,‘perm_delete‘:0,‘resid‘:‘sc20150512034635‘,‘memberid‘:‘sc155489a07e0e24‘,‘invited‘:1}]}"; 7  8             StringEntity se = new StringEntity(str, ContentType.create("application/json", "UTF-8")); 9             hp.setEntity(se);10 11             System.out.println(EntityUtils.toString(se));12 13             CloseableHttpResponse response1 = null;14             try {15                 response1 = httpclient.execute(hp);16 17                 System.out.println(response1.getStatusLine());18                 HttpEntity entity1 = response1.getEntity();19 20                 // do something useful with the response body21                 // and ensure it is fully consumed22                 // EntityUtils.consume(entity1);23 24                 String jsonstr = EntityUtils.toString(entity1);25 26                 System.out.println(jsonstr);27 28             } catch (ClientProtocolException e) {29                 e.printStackTrace();30             } catch (IOException e) {31                 e.printStackTrace();32             } finally {33                 response1.close();34             }35 36         } finally {37             httpclient.close();38         }39     }

 

結果:

 

但是同樣的Json, 使用C#的HTTPClient就完全沒有問題.

計較了一下fiddle抓圖:

使用C# HttpClient: (value是能取到)

 

使用Java Apache HttpClient (Value取不到)

 

比較一下就發現, 在Raw視圖, 才可以看出, HttpRequest的Body是有不同的. 就是一對引號. "".

 

所以把Java的代碼中加一行:

str ="\"" +str + "\"";

 

WebAPI端就正常拿到Value了.

 

估計有更深層次的解決方案, 暫且先記一筆.

 

使用Java HttpComponent/HttpClinet 調用 WebAPI問題的解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.