反序列化http响应

时间:2013-02-27 14:26:52

标签: c# asp.net asp.net-web-api

我正在消耗(不是我的)用肥皂结构构建的api。

<?xml version='1.0' encoding='UTF-8'?>
 <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
     <somePrefix:someNodeName xmlns="ulrWithNoPrefix" xmlns:somePrefix="urlWithPrefix"
    xmlns:anotherPrefix="UrlForAnotherprefix xmlns:AttributePrefix="UrlForAttributePrefix>
     <anotherPrefix:childNode1>value</anotherPrefix:childNode1>
     <anotherPrefix:childNode2>value</anotherPrefix:childNode2>
     <anotherPrefix:childNode3>value</anotherPrefix:childNode3>
     <anotherPrefix:childNode3 AttributePrefix:AttributeName="attributeValue/>
  </somePrefix:someNodeName>
</S:Body>
</S:Envelope>

我正在尝试从中构建一个C#对象。 我确定我可以继续每个节点,提取内部文本并填充自定义类型,但我明白这可以完成(我将需要建立类型)。 我试图在httpClient中使用内置的反序列化机制,并且还下载了protobuf-net并试试了我的运气但也失败了。

我有以下方法来处理连接:

public void RunPostTask(string url, string req) { string res = ""; var client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(); request.Content = new StringContent(req); request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");

var task = client.PostAsync(url, request.Content).ContinueWith((TaskResponse) => { TaskResponse.Result.EnsureSuccessStatusCode(); var response = TaskResponse.Result; var ReadTask = response.Content.ReadAsStringAsync(); ReadTask.Wait(10000); res = ReadTask.Result; }).ContinueWith((err) => { if (err.Exception != null) { res = err.Exception.ToString(); } }); task.Wait(10000); }

我想反序列化res字符串(上面显示了一个典型的结构)

还想知道我使用HttpClient的方式是否正确,还是更好的设计?

由于 吉拉德

0 个答案:

没有答案