Example of HTTP Post?

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Anywhere I can find a good example of an Http Post?

In particular, I think UrlEncode doesn't work in .Net CF. No example I found
addressed that.

I'd like to pass some data to a url through parameters. GET might be fine
too if I can encode the parameters.

I'm hoping blindly that this will work more reliably than Web Services have
for this situation.

Nathan
 
Here's the code peace from one of my apps:

public string SendRequest(string url, string parameters)
{
StreamReader reader = null;
HttpWebResponse response = null;
string result = String.Empty;

try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType="application/x-www-form-urlencoded";
request.ContentLength = parameters.Length;
request.Timeout = 10000;

Stream requestStream = request.GetRequestStream();

requestStream.Write(Encoding.ASCII.GetBytes(parameters), 0,
parameters.Length);

requestStream.Close();

response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
response.Close();
}
catch(WebException ex)
{
string message = ex.Status.ToString();
HttpWebResponse resp = (HttpWebResponse)ex.Response;
if(null != resp)
{
message = resp.StatusDescription;
resp.Close();
}
return GenerateErrorXml("Communication error:" + message);
}
catch(Exception ex)
{
return GenerateErrorXml("Communication error:"+ ex.Message);
}
finally
{
if (response != null)
{
response.Close();
}
}

return result;
}
 
Thanks.

What does the parameters string look like beforehand? Is it delimited and
are any characters escaped.

Nathan
 
Back
Top