簡體   English   中英

調用HTTPWebRequest時無效的URI

[英]Invalid URI when calling 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+"
</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();

當我運行此代碼時,我收到錯誤說明:

Invalid URI: The URI scheme is not valid.

如何通過WebRequest進行補救,以發送格式正確的XML並以XML格式接收響應?

正如錯誤和文檔清楚指出的那樣, WebRequest.Create采用URL,而不是POST負載。

您需要將URL傳遞給Create() ,並將XML有效負載寫入請求流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM