c# - 向 url 发送 xml 请求并接收返回的 xml 响应

标签 c# asp.net-mvc xmlhttprequest httpwebrequest usps

我正在尝试向 url 发送 xml 请求,并且响应也将是 xml 响应。我知道如何从 MVC 应用程序调用服务端点,但我不确定如何调用此 url 以及如何读取它将返回的内容。这是我到目前为止所拥有的。这是正确的方向吗?

请求:

<CityStateLookupRequest USERID=”xxxxxxxx”>
<ZipCode ID="0">
<Zip5>90210</Zip5>
</ZipCode>
</CityStateLookupRequest>

回应:

<CityStateLookupResponse>
<ZipCode ID="0">
<Zip5>90210</Zip5>
<City>BEVERLY HILLS</City>
<State>CA</State>
</ZipCode>
</CityStateLookupResponse>

C# 代码:

var xmlRequest = new XElement("CityStateLookupRequest",
                new XAttribute("USERID", "XXXXXXXXX"),
                new XElement("ZipCode",
                    new XAttribute("ID", "0"),
                    new XElement("Zip5", "43065")));    

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(" http://production.shippingapis.com/ShippingAPI.dll");

byte[] bytes = System.Text.Encoding.ASCII.GetBytes(xmlRequest.ToString());
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
    var xmlDoc = new XmlDocument();

    xmlDoc.Load(response.GetResponseStream());
}

最佳答案

这是您可以执行此操作的一种方法,它基本上是一个表单帖子。

var xmlRequest = new XElement("CityStateLookupRequest",
    new XAttribute("USERID", "XXXXXXXXX"),
    new XElement("ZipCode",
        new XAttribute("ID", "0"),
        new XElement("Zip5", "43065")));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://production.shippingapis.com/ShippingAPI.dll");        

// parameters to post - other end expects API and XML parameters
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("API", "CityStateLookup"));
postData.Add(new KeyValuePair<string, string>("XML", xmlRequest.ToString()));    

// assemble the request content form encoded (reference System.Net.Http)
HttpContent content = new FormUrlEncodedContent(postData);

// indicate what we are posting in the request
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = content.Headers.ContentLength.Value;
content.CopyToAsync(request.GetRequestStream()).Wait();                        

// get response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
    // as an xml: deserialise into your own object or parse as you wish
    var responseXml = XDocument.Load(response.GetResponseStream());
    Console.WriteLine(responseXml.ToString());
}

关于c# - 向 url 发送 xml 请求并接收返回的 xml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932076/

相关文章:

c# - 转换 <V :shape>/Embed to a w:drawing

c# - 自定义 HttpHandler 未触发,在 ASP.NET MVC 应用程序中返回 404

javascript - "Content-Disposition", "inline;filename="+ fileName 不强制显示 pdf 文件

AngularJS 在 POST 上从 https 请求 http

javascript - 无法将 XMLHttpRequest 发送到 3 个单独的 API 网址

c# - 在两个数据库中保存和更新?

c# - 在 Asp.net 中使用 Linq 创建 A-Z 索引的最佳实践

c# - 如何验证玩家当前是否登录(或登录)到 iOS Game Center?

javascript - 如何查询XMLHttpRequest的方法类型?

c# - 如何使用 NinjectHttpApplication(无 nuget)在 HttpModule 中注入(inject)依赖项?