我正在尝试使用 Apache 组件 (4.3) 的示例构建一个 http POST - http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fluent.html .不幸的是,我收到一个错误,我无法找到解决方法。
我以前使用过以前的 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()
方法 - 但我遇到了同样的错误。
最佳答案
我遇到了同样的问题,解决方法是使用 Apache Client 4.3.1,它可以正常工作。
似乎请求已更改:
- 在 4.3.1 他们使用 public
HttpRequestBase
- 在最新版本中,他们使用 protected 包
InternalHttpRequest
关于java - 如何使用流利的Apache Components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095341/