この同じ問題に関する多くの質問を見て、提案された多くの解決策を試しましたが、成功しませんでした。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 :エンティティを設定し、エンティティに名前と値のペアを与えることで、パラメーターを渡すことができます。ただし、前述のように、これは content-type をホースします。完全にあきらめる前に、他に考えることはありますか?