我正在嘗試使用Apache組件(4.3) - http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fluent.html的示例構建http POST。不幸的是,我收到一個錯誤,我無法找到如何解決。如何使用流暢的Apache組件
我以前使用過前HttpClient
- 所以這是我第一次使用組件。
下面是代碼片段:
String address = "http://1.1.1.1/services/postPositions.php";
String response = Request.Post(address)
.bodyString("Important stuff", ContentType.DEFAULT_TEXT)
.execute().returnContent().asString();
System.out.println(response);
,當我運行代碼,我得到一個異常:
Exception in thread "main" java.lang.IllegalStateException: POST request cannot enclose an entity
at org.apache.http.client.fluent.Request.body(Request.java:299)
at org.apache.http.client.fluent.Request.bodyString(Request.java:331)
at PostJson.main(PostJson.java:143)
我試圖建立一個表單元素,以及與使用bodyForm()
方法 - 但我得到相同的錯誤。
措辭不清。你是說你***有同樣的問題,這是解決方案,還是你說你有同樣的問題,仍然在尋找解決方案? –
澄清答案。 – swKK
這個和其他答案幫助了我。但是,由於這個答案帶給我一個解決方法,我決定將其標記爲正確的答案;-) –