简体   繁体   中英

C# equivalent of Visual Basic API call

I have inherited a API which gives a Visual Basic example of how to call the API which is below:

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

I am familar with using HttpWebRequest and have no issue setting the content type, method etc but I am not sure how to set the sXMLData - which property of my HttpWebRequest would I set?

Thanks in Advance.

It looks like you'd want to write that XML data to the request body. To do that, you normally create a StreamWriter using HttpWebRequest.GetRequestStream() :

// HttpWebRequest request;
// string sXmlData;

using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
{
    sw.Write(sXmlData);
}

It should be as simple as calling UploadString on a WebClient :

using (WebClient wc = new WebClient()) {
    wc.Headers[HttpRequestHeader.ContentType] = "text/xml";
    wc.UploadString(sAction, sXMLData); // (url, data) .. default method is POST
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM