HttpWebRequest and HttpWebResponse - here are the two classes to simulate the web browser funtionality in smart client applications.
We can use the following method when we had to call a web page which return the result as Xml.
public static string GetHttpRequest(StringBuilder data, string sourcePath)
{
Stream aStream = null;
try
{
//Send POST request web page
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourcePath);
req.Method = "POST";
// Content type is xml
req.ContentType = "text/xml";
req.Credentials = CredentialCache.DefaultCredentials;
if (data.Length > 0)
{
req.ContentLength = data.Length;
StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write(data.ToString());
sw.Flush();
sw.Close();
}
// Create The Response Object And Fill It By Sending The Request;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
aStream = response.GetResponseStream();
StreamReader sr = new StreamReader(aStream);
StringBuilder sbOutput = new StringBuilder();
char[] buffer = new char[1024];
int r;
while ((r = sr.Read(buffer, 0, buffer.Length)) > 0)
sbOutput.Append(buffer, 0, r);
return sbOutput.ToString();
}
catch (WebException ex)
{
//TODO: handle exception
}
catch (Exception ex)
{
//TODO: handle exception
}
finally
{
aStream.Close();
}
return string.Empty;
}
