简体   繁体   English

将xml请求发送到url并接收xml响应

[英]Sending xml request to url and receiving xml response back

I'm trying to send an xml request to a url and the response will also be an xml response. 我正在尝试将xml请求发送到url,并且响应也将是xml响应。 I know how to call a service endpoint from an MVC application but I'm not sure how to call this url and how to read what it will give me back. 我知道如何从MVC应用程序调用服务终结点,但是我不确定如何调用此URL以及如何阅读它将给我带来什么。 This is what I have so far. 到目前为止,这就是我所拥有的。 Is this in the right direction? 这是朝正确的方向吗?

Request: 请求:

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

Response: 响应:

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

C# Code: 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());
}

Here is one way you could do this, which is basically a form post. 这是您可以执行此操作的一种方法,基本上是一种表单发布。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM