Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用HTTPWebRequest时URI无效_C#_Xml_Httpwebrequest - Fatal编程技术网

C# 调用HTTPWebRequest时URI无效

C# 调用HTTPWebRequest时URI无效,c#,xml,httpwebrequest,C#,Xml,Httpwebrequest,我编写了一个控制台应用程序,它调用RESTful web服务,通过HTTP发送XML请求并返回XML响应。以下是我当前使用的代码: WebRequest wrq = WebRequest.Create("<?xml version=1.0 encoding=ISO-8859-1?> <Request xmlns=https://sapiqa.overstock.com/api><MerchantKey>"+MerchantKey+" </Merchant

我编写了一个控制台应用程序,它调用RESTful web服务,通过HTTP发送XML请求并返回XML响应。以下是我当前使用的代码:

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

如何通过WebRequest进行补救,以发送格式正确的XML并接收XML响应?

正如错误和文档明确指出的那样,
WebRequest.Create
采用URL,而不是POST有效负载


您需要将URL传递到
Create()
,并将XML有效负载写入请求流。

生成POST请求需要更多的工作。看见或者搜索[httpwebrequest post]。@JimMischel您能在回答中进一步阐述一下吗?上的示例非常详细。事实上有数百个例子。在http://code>httpwebrequest post-example上进行简单的谷歌搜索,将提供大量示例供您选择。@JimMischel感谢您的链接。我仍然不清楚如何在POST请求中发送我的MerchantKey和AuthenticationKey值?我也不知道如何使用RESTAPI。我们已经向您展示了如何提出POST请求。由您决定如何格式化有效负载(您的数据),以便服务器接受它。也许您可以找到一些API调用的文档。那么,我该如何构建您提到的XML负载呢?如果Crete方法采用一个简单的URL,那么我如何传入MerchantKey和AuthenticationKey变量值?@SidC-看看Jim Mischel上面链接的答案。
Invalid URI: The URI scheme is not valid.