【问题标题】:Using HttpPost/Put with ContentType Java Apache将 HttpPost/Put 与 ContentType Java Apache 一起使用
【发布时间】:2013-08-21 18:04:03
【问题描述】:

我查看了许多关于同一问题的问题,并尝试了许多建议的解决方案,但均未成功。位于here 的那个似乎最相关,因为 RedSonic 的评论回答了为什么这不起作用。
我正在尝试使用 Java 的 Apache 库向我的服务器发出 POST 或 PUT 请求。

public static void put(String caseNumber,DefaultHttpClient httpClient)
        throws Exception {
    HttpPut putRequest = new HttpPut(
            "http://localhost:8888/servlet/example1/" + caseNumber);
    putRequest.addHeader("Version", version);
//      putRequest.setHeader("Content-Type", content);
//      putRequest.addHeader(new BasicHeader("ContentType", content));
    putRequest.setHeader("Accept", accept);
    putRequest.setHeader("Date", date);

    List <NameValuePair> nvpsPut = new ArrayList <NameValuePair>();
    nvpsPut.add(new BasicNameValuePair("group", jsonPostGroup));
    putRequest.setEntity(new UrlEncodedFormEntity(nvpsPut));

    HttpResponse response = httpClient.execute(putRequest);

}

我尝试创建一个单独的实体并设置实体的内容类型,然后将实体设置为请求。我也尝试了其他方法,但如果我尝试发送的参数变为空白,我无法获得具有我想要传入的内容类型的请求。

也试过了

HttpPut putRequest = new HttpPut("http://localhost:8888/servlet/example1/" + caseNumber);
putRequest.addHeader("Version", version);
putRequest.setHeader("Content-Type", content);
putRequest.setHeader("Accept", accept);
putRequest.setHeader("Date", date);

BasicHttpParams bhp = new BasicHttpParams();
bhp.setParameter("test", jsonPostGroup);
putRequest.setParams(bhp);

HttpResponse response = httpClient.execute(putRequest);

日志:

BasicClientConnectionManager - Get connection for route {}->http://localhost:8888
DefaultClientConnectionOperator - Connecting to localhost:8888
RequestAddCookies - CookieSpec selected: best-match
RequestAuthCache - Auth cache not set in the context
RequestTargetAuthentication - Target auth state: UNCHALLENGED
RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
DefaultHttpClient - Attempt 1 to execute request
DefaultClientConnection - Sending request: PUT /servlet/example1/000001994 HTTP/1.1
wire - >> "PUT /servlet/example1/000001994 HTTP/1.1[\r][\n]"
wire - >> "Version: 1.1[\r][\n]"
wire - >> "Content-Type: application/JSON[\r][\n]"
wire - >> "Accept: vnd.siren+JSON[\r][\n]"
wire - >> "Date: Wed Aug 21 15:30:00 EDT 2013[\r][\n]"
wire - >> "Content-Length: 0[\r][\n]"
wire - >> "Host: localhost:8888[\r][\n]"
wire - >> "Connection: Keep-Alive[\r][\n]"
wire - >> "User-Agent: Apache-HttpClient/4.2.5 (java 1.5)[\r][\n]"
wire - >> "[\r][\n]"
headers - >> PUT /servlet/example1/000001994 HTTP/1.1
headers - >> Version: 1.1
headers - >> Content-Type: application/JSON
headers - >> Accept: vnd.siren+JSON
headers - >> Date: Wed Aug 21 15:30:00 EDT 2013
headers - >> Content-Length: 0
headers - >> Host: localhost:8888
headers - >> Connection: Keep-Alive
headers - >> User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
wire - << "HTTP/1.1 200 OK[\r][\n]"
wire - << "Set-Cookie: JSESSIONID=davwq58qgxr8yvyw7bfybbbv;Path=/[\r][\n]"
wire - << "Expires: Thu, 01 Jan 1970 00:00:00 GMT[\r][\n]"
wire - << "Content-Type: text/html; charset=ISO-8859-1[\r][\n]"
wire - << "Content-Length: 0[\r][\n]"
wire - << "Server: Jetty(9.0.0.v20130308)[\r][\n]"
wire - << "[\r][\n]"
DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
headers - << HTTP/1.1 200 OK
headers - << Set-Cookie: JSESSIONID=davwq58qgxr8yvyw7bfybbbv;Path=/
headers - << Expires: Thu, 01 Jan 1970 00:00:00 GMT
headers - << Content-Type: text/html; charset=ISO-8859-1
headers - << Content-Length: 0
headers - << Server: Jetty(9.0.0.v20130308)
ResponseProcessCookies - Cookie accepted: "[version: 0][name: JSESSIONID][value: davwq58qgxr8yvyw7bfybbbv][domain: localhost][path: /][expiry: null]". 
DefaultHttpClient - Connection can be kept alive indefinitely
BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@7842f3a9
BasicClientConnectionManager - Connection can be kept alive indefinitely

编辑:我发现了一些我尝试过的其他建议。例如在 httpclient 而不是请求上设置参数(在这种情况下没有多大意义),但我遇到了同样的问题。

编辑 8/23:我仍然可以通过设置实体来传递参数,并为实体提供名称 + 值对。但是,如前所述,这会影响内容类型。在我完全放弃之前还有什么想法吗?

【问题讨论】:

  • 首先,您通过第二次调用setEntity() 来覆盖您的HttpEntity。请求参数不是实体。可以使用AbstractHttpMessagesetParams(HttpParams)方法(即通过继承HttpPost)来传递请求参数。
  • 对不起,第一个 setEntity 是我在尝试一堆不同的东西时剩下的。我现在将其编辑出来。我最初尝试过 setParams 但没有成功。我刚刚再次尝试,并将该代码添加到原始问题中。
  • 第二个代码 sn-p 中的内容似乎应该可以工作。尝试启用日志记录并检查发送的内容和未发送的内容。见here
  • 我启用了日志记录,但它没有提及 BasicHttpParams 的名称或值。它还提到内容长度等于零。我在日志中没有看到任何说明原因的内容,但我会尝试找出答案。我尝试发布日志,但它的格式真的很差。不知道您对此是否有任何建议。
  • 如果您编辑代码,在文本框上方的横幅中,有{} 代码大括号。选择您的日志文本并单击它。

标签: java apache http-post httpclient


【解决方案1】:

这是一个老问题,但您不应该添加所有标题吗? 有些调用是setHeader:

putRequest.addHeader("Version", version);
putRequest.setHeader("Content-Type", content);

应该是:

putRequest.addHeader("Version", version);
putRequest.addHeader("Content-Type", content);

【讨论】:

  • 我记得在这个 API 的情况下,标头是需要的,但不是必需的。实际上,由于编写 API 的底层语言(Business Basic w/Java)中的错误,提供标头会破坏应用程序。我早就摆脱了这种情况!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2012-10-04
  • 2011-09-12
  • 2020-01-17
  • 2012-04-26
相关资源
最近更新 更多