【发布时间】:2016-04-05 18:37:08
【问题描述】:
我有一个用 VS2010 编写的已部署的 android 应用程序,我正在移植到 VS2012。我正在尝试使用 HttpClient 与我的 WCF Restful web 服务进行通信。 Web 服务是一种 XML Web 服务。我使用 XmlSerializer 将模型对象序列化为字节数组。
在 VS2010 中,我成功地使用了这样的 WebClient:
using (WebClient myWebClient = new WebClient())
{
myWebClient.Headers.Add("Content-Type", "application/xml");
myWebClient.UploadData(uri, postBytes);
}
但是我不知道如何使用 HttpClient 发布相同的字节数组。这是我目前所拥有的:
var client = new HttpClient()
client.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var content = new ByteArrayContent(postBytes);
Task<HttpResponseMessage> task = client.PostAsync(uri,content);
task.Wait();
HttpResponseMessage message = task.Result;
webservice 方法具有以下属性:
[WebInvoke(Method="POST", RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
UriTemplate = "/Order.Xml/Process")]
[XmlSerializerFormat]
响应只是 400 Bad Request。似乎发布的内容格式不正确。但我不知道该怎么做才能让它与现有的 Web 服务一起工作。它与 WebClient 一起正常工作。
【问题讨论】:
-
那么实际的服务器期望一个字节数组作为正文而不是xml中的实际序列化对象?
-
Web 服务需要一个 Order 对象。 Web 服务负责获取字节数组并反序列化为模型对象。
标签: xamarin xamarin.android androidhttpclient