ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse httpResponse = null;
//Parameter string should look like "parameter1=value1¶meter2=value2"
byte[] btParameters = encoding.GetBytes(sParameterString);
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = btParameters.Length;
httpRequest.Timeout = 30000;
//Create the request
using (HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(this.URL + "/" + this.WebServiceMethodName))
{
//Add parameters
using (Stream requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(btParameters, 0, btParameters.Length);
}
//Send the request
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
//Process the response
string response;
using (Stream baseStream = httpResponse.GetResponseStream())
{
using (System.IO.StreamReader responseStreamReader = new System.IO.StreamReader(baseStream))
{
//The response
response = responseStreamReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
//Error
}
finally
{
if (httpResponse != null)
httpResponse.Close();
}
}
|