2011-04-04 58 views
0

更新:這些問題是由反向代理執行301重定向造成的。將網址更改爲重定向目標可以解決問題。IIS似乎認爲Android HttpPost是GET

我很努力地從android向web服務發出POST請求。

我有以下IIS7上運行的Web服務:

<OperationContract()> _ 
<Web.WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, Method:="POST", RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="HelloWorld")> _ 
    Function HelloWorld() As XmlElement 

當我發送POST請求到這個URL從Firefox它按預期工作。

當我使用下面的代碼使來自Android設備的要求:

String sRequest = "http://www.myserviceurl.com/mysevice/HelloWorld"; 
ArrayList<NameValuePair> arrValues = new ArrayList<NameValuePair>(); 
arrValues.add(new BasicNameValuePair("hello", "world")); 

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpRequest = new HttpPost(sRequest); 
httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
httpRequest.setEntity(new UrlEncodedFormEntity(arrValues)); 
HttpResponse response = httpClient.execute(httpRequest); 

我得到一個不允許的方法405響應,並期待在IIS時登錄請求這個網址顯示爲「GET 」。

如果我將請求的目標更改爲反映$ _SERVER ['REQUEST_METHOD']的PHP腳本,則輸出爲POST。

Web服務的web.config具有GET,HEAD和POST作爲動詞。

有什麼我忽略了嗎?

+0

所有證據似乎表明正在通過該服務背後坐着的反向代理問題造成的。 – rpcutts 2011-04-06 09:13:09

+0

Web服務位於逆向代理的後面。我的服務的網址被重定向到另一個網址,這個重定向是一個GET而不是POST。因此,405. – rpcutts 2011-04-06 11:19:48

回答

0

嘗試:

DefaultHttpClient http = new DefaultHttpClient(); 
    HttpResponse res; 
    try { 
     HttpPost httpost = new HttpPost(s); 
     httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.DEFAULT_CONTENT_CHARSET)); 

     res = http.execute(httpost); 


     InputStream is = res.getEntity().getContent(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while((current = bis.read()) != -1){ 
       baf.append((byte)current); 
     } 
     res = null; 
     httpost = null; 
     String ret = new String(baf.toByteArray(),encoding); 
     return ret; 
     } 
    catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     return e.getMessage(); 
    } 
    catch (IOException e) { 
     // TODO Auto-generated catch block 
     return e.getMessage(); 
    } 
+0

我不明白這個代碼中的請求與我的不同? 您的 res = http.execute(httpost); 礦井 response = httpClient.execute(httpRequest); 兩者都是HttpResponse = DefaultHttpClient.execute(HttpPost) 這是執行返回405錯誤。 我的密度過高嗎? – rpcutts 2011-04-04 14:17:29

+0

我可以在http.execute之前看到的唯一區別是包含HTTP.DEFAULT_CONTENT_CHARSET – rpcutts 2011-04-04 14:23:00

2

我不得不通過禁用自動重定向,然後捕捉響應代碼和重定向URL,並重新執行POST來實現一種變通方法。

// return false so that no automatic redirect occurrs 
httpClient.setRedirectHandler(new DefaultRedirectHandler() 
{ 
    @Override 
    public boolean isRedirectRequested(HttpResponse response, HttpContext context) 
    { 
     return false; 
    } 
}); 

然後,當我發出的請求

response = httpClient.execute(httpPost, localContext); 
int code = response.getStatusLine().getStatusCode(); 
// if the server responded to the POST with a redirect, get the URL and reexecute the POST 
if (code == 302 || code == 301) 
{ 
    httpPost.setURI(new URI(response.getHeaders("Location")[0].getValue())); 
    response = httpClient.execute(httpPost, localContext); 
}