我正在尝试使用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
这个和其他答案帮助了我。但是,由于这个答案带给我一个解决方法,我决定将其标记为正确的答案;-) –