andrewb
andrewb

Reputation: 3095

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.

Upvotes: 3

Views: 322

Answers (2)

Simon Whitehead
Simon Whitehead

Reputation: 65079

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
}

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102753

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

Upvotes: 2

Related Questions