我已經看過很多關於這個問題的問題,並嘗試了許多提議的解決方案,但都沒有成功。位於here的那個似乎是最相關的,因爲RedSonic的評論回答了爲什麼這不起作用。
我正在嘗試使用Java的Apache庫向我的服務器發出POST或PUT請求。使用HttpPost /放置ContentType Java Apache
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 [email protected]
BasicClientConnectionManager - Connection can be kept alive indefinitely
編輯:我發現,我已經嘗試了一些其他建議。比如在httpclient上設置參數而不是請求(在這種情況下這沒什麼意義),但我遇到了同樣的問題。
編輯8/23:我仍然可以通過設置實體來傳遞參數,並給實體一個名稱+值對。但是,如上所述,這將軟化內容類型。在我完全放棄之前有更多想法嗎?
首先,你可以通過調用'setEntity()'第二次重寫你的'HttpEntity'。請求參數不是一個實體。你可以使用'AbstractHttpMessage'的'setParams(HttpParams)'方法(即通過繼承'HttpPost')來傳遞請求參數。 –
對不起,當我嘗試一堆不同的東西時,第一個setEntity是剩下的。我現在編輯它。我曾嘗試setParams最初沒有成功。我只是再次嘗試,並將該代碼添加到原始問題。 – user2402831
你在你的第二個代碼片段中看起來應該有效。嘗試啓用日誌記錄並檢查發送的內容和不發送的內容。看[這裏](http://stackoverflow.com/questions/3246792/how-to-enable-logging-for-apache-commons-httpclient-on-android)。 –