更新:这些问题是由执行 301 重定向的反向代理引起的。将 url 更改为重定向的目标解决了该问题。
我正在努力从 android 向网络服务发出 POST 请求。
我有一个在 IIS7 上运行的 Web 服务,其内容如下:
<OperationContract()> _
<Web.WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, Method:="POST", RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="HelloWorld")> _
Function HelloWorld() As XmlElement
当我从 Firefox 向此 url 发送 POST 请求时,它按预期工作。
当我使用以下代码从 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);
我收到 Method Not Allowed 405 响应,并且在查看 IIS 日志时,对该 url 的请求显示为“GET”。
如果我将请求的目标更改为回显 $_SERVER['REQUEST_METHOD'] 的 PHP 脚本,则输出为 POST。
Web 服务的 web.config 具有 GET、HEAD 和 POST 作为动词。
有什么我忽略的吗?