我继承了一个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的哪个属性?
先谢谢。
答案 0 :(得分:2)
您似乎希望将XML数据写入请求正文。为此,您通常使用HttpWebRequest.GetRequestStream()
:
// HttpWebRequest request;
// string sXmlData;
using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
{
sw.Write(sXmlData);
}
答案 1 :(得分:0)
应该像在UploadString
上调用WebClient
一样简单:
using (WebClient wc = new WebClient()) {
wc.Headers[HttpRequestHeader.ContentType] = "text/xml";
wc.UploadString(sAction, sXMLData); // (url, data) .. default method is POST
}