3

我继承了一个 API,它提供了一个如何调用 API 的 Visual Basic 示例,如下所示:

Dim sPost As String
Dim sAction As String
Dim sXMLData As String
Dim sHTTPHeaders As String
sPost = "POST"
sAction = "http://MyHost/1/XmlService"
sXMLData = "<xml ..> <request …….. /></xml>"
sHTTPHeaders = "Content-type: text/xml"
Inet1.Execute sAction, sPost, sXMLData, sHTTPHeaders

我熟悉使用 HttpWebRequest 并且在设置内容类型、方法等方面没有问题,但我不确定如何设置 sXMLData - 我应该设置 HttpWebRequest 的哪个属性?

提前致谢。

4

2 回答 2

2

看起来您希望将该 XML 数据写入请求正文。为此,您通常使用以下命令创建 StreamWriter HttpWebRequest.GetRequestStream()

// HttpWebRequest request;
// string sXmlData;

using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
{
    sw.Write(sXmlData);
}
于 2013-09-19T02:29:01.003 回答
0

它应该像调用UploadStringa一样简单WebClient

using (WebClient wc = new WebClient()) {
    wc.Headers[HttpRequestHeader.ContentType] = "text/xml";
    wc.UploadString(sAction, sXMLData); // (url, data) .. default method is POST
}
于 2013-09-19T02:30:49.170 回答