我有一个 RESTful WCF 服务,它的一种方法使用对象作为参数
[WebInvoke(UriTemplate = "save", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat= WebMessageFormat.Xml), OperationContract]
public SampleItem Create(SampleItem instance)
{
return new SampleItem() { Id = 1, StringValue = "saved" };
// TODO: Add the new instance of SampleItem to the collection
//throw new NotImplementedException();
}
我正在尝试从我的 eclipse android 项目中调用此方法。我正在使用这些代码行
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post=new HttpPost("http://10.0.2.2:2768/Service1.svc/save");
ArrayList<NameValuePair> nvp= new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("Id", "1"));
nvp.add(new BasicNameValuePair("StringValue", "yolo"));
post.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse httpResponse = httpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
String xml = EntityUtils.toString(httpEntity);
每次我Method not allowed.
在服务方法返回的 XML 中收到此错误时。
我尝试从浏览器调用它,但在那里遇到了同样的错误。
请告诉我我做错了什么以及我能做些什么。
提前感谢任何可以提供帮助的人。
注意:其他不使用对象作为参数的方法工作正常。
编辑:成功尝试了 Fiddler2。但又停滞了。
我尝试SampleItem Create(SampleItem instance)
使用 url 调用该方法http://localhost:2768/Service1.svc/save
并且它有效。该方法以 XML 格式返回对象。
在提琴手中,我将请求正文添加为
<SampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>1</Id><StringValue>saved</StringValue></SampleItem>
但问题是我找不到任何方法将此 xml 字符串添加到HttpPost或HttpRequest作为requestbody eclipse android 项目。
注意:将 xml 字符串作为Header或UrlEncodedFormEntity传递不起作用。