1

我查看了许多关于同一问题的问题,并尝试了许多建议的解决方案,但均未成功。位于此处的那个似乎最相关,因为 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:我仍然可以通过设置实体来传递参数,并为实体提供名称 + 值对。但是,如前所述,这会影响内容类型。在我完全放弃之前还有什么想法吗?

4

1 回答 1

1

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

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

应该:

putRequest.addHeader("Version", version);
putRequest.addHeader("Content-Type", content);
于 2017-06-30T10:47:32.693 回答