我已经看过很多关于同样问题的问题,并尝试了许多提出的解决方案,但没有成功。位于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 :我仍然可以通过设置实体传递参数,并为实体提供名称+值对。但是,如上所述,这会占用内容类型。在我完全放弃之前还有更多想法吗?
答案 0 :(得分:0)
这是一个老问题,但是你不应该添加所有标题吗? 一些调用是setHeader:
putRequest.addHeader("Version", version);
putRequest.setHeader("Content-Type", content);
应该是:
putRequest.addHeader("Version", version);
putRequest.addHeader("Content-Type", content);