Monday, August 3, 2009

call and execute a method using HttpWebRequest

Here is test code will call and execute a method in some server and retrive xml output.


string Qstring = "name=Alex&age=20";
string url = "https://mywebserver.com/websites.exe";
string data = Qstring; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";

StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

strMVRXML = reader.ReadToEnd(); response.Close();
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.LoadXml(strXML);

No comments: