【发布时间】:2012-06-07 09:20:02
【问题描述】:
我有一个 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);
每次我在服务方法返回的 XML 中收到此错误 Method not allowed.。
我尝试从浏览器调用它,但在那里遇到了同样的错误。
请告诉我我做错了什么以及我能做些什么。
提前感谢任何可以提供帮助的人。
注意:其他不使用对象作为参数的方法工作正常。
编辑:成功尝试了 Fiddler2。但又停了下来。
我尝试使用 URL http://localhost:2768/Service1.svc/save 调用方法 SampleItem Create(SampleItem instance) 并且它有效。该方法以 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 传递不起作用。
【问题讨论】:
标签: android http-post wcf-rest webinvoke