[英]Invalid URI when calling HTTPWebRequest
I've written a console app that calls a RESTful web service sending XML requests over HTTP which returns XML responses. 我编写了一个控制台应用程序,该应用程序调用RESTful Web服务,该服务通过HTTP发送XML请求,并返回XML响应。 Here's the code I'm currently using:
这是我当前正在使用的代码:
WebRequest wrq = WebRequest.Create("<?xml version=1.0 encoding=ISO-8859-1?>
<Request xmlns=https://sapiqa.overstock.com/api><MerchantKey>"+MerchantKey+"
</MerchantKey><AthenticationKey>"+AuthenticationKey+"</AuthenticationKey><"+APIMethod+"
/></Request>");
wrq.Method = "POST";
// Create POST data and convert it to a byte array. Strip out unnecessary text.
byte[] byteArray = Encoding.UTF8.GetBytes(URL.Replace(APIMethod, ""));
// Set the ContentType property of the WebRequest.
wrq.ContentType = "text/xml";
// Set the ContentLength property of the WebRequest.
wrq.ContentLength = byteArray.Length;
Stream dataStream = wrq.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = wrq.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
GetOrders2Response = responseFromServer;
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadKey();
When I run this code, I receive the error stating: 当我运行此代码时,我收到错误说明:
Invalid URI: The URI scheme is not valid.
How do I remedy by WebRequest to send properly formatted XML and receive a response in XML as well? 如何通过WebRequest进行补救,以发送格式正确的XML并以XML格式接收响应?
As the error and documentation clearly state, WebRequest.Create
takes a URL, not a POST payload. 正如错误和文档清楚指出的那样,
WebRequest.Create
采用URL,而不是POST负载。
You need to pass the URL to Create()
, and write the XML payload to the request stream. 您需要将URL传递给
Create()
,并将XML有效负载写入请求流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.