0
我試圖發送一個xml請求到一個url並且響應也將是一個xml響應。我知道如何從MVC應用程序中調用服務端點,但我不知道如何調用此URL以及如何閱讀它將使我回饋的內容。這是我迄今爲止所擁有的。這是正確的方向嗎?發送xml請求到url並收到xml響應
請求:
<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());
}
謝謝你,這是我需要的。 – NNassar 2015-03-11 13:17:26