I'm into uncharted territory - I have to create an httppost for the first time (xml). I've looked at plenty of examples but to be frank, its kinda confusing for a noob.
The function I'm currently working on is this:
public string SubmitRequest(string postUrl, string contentType, string postValues)
{
var req = WebRequest.Create(postUrl);
req.Method = "POST";
req.ContentType = contentType;
try
{
using (var reqStream = req.GetRequestStream())
using (var writer = new StreamWriter(reqStream))
{
writer.WriteLine(postValues);
}
var resp = req.GetResponse();
using (var respStream = resp.GetResponseStream())
using (var reader = new StreamReader(respStream))
{
return reader.ReadToEnd().Trim();
}
}
catch(WebException ex)
{
// do something here
}
return string.Empty;
}
I suppose I have 2 questions:
Does the code seem correct in terms of correctly disposing/closing object? Is this the most effective way of writing my httppost given I'm using asp.net 4.0?
Thanks in advance