How to read server response in C#.NET

-------------------------------------------------------
Goto blog home
Visit my new blog dedicated to Internet of Things, Embedded Programming & Automation
-------------------------------------------------------

The following snippet shows how to read the text/html/xml response from a remote server through .NET code. This technique can be employed as part of a page scrapping service also.

string _strContent = String.Empty;
StreamReader _objSR;
WebResponse _objResponse = null;
WebRequest _objRequest = HttpWebRequest.Create("http://domain.server/Proxy/Controller.aspx?Op=someoperand");
int _contentLength = 0;
_objRequest.Method = "GET";
_objRequest.Credentials = CredentialCache.DefaultCredentials;
_objResponse = _objRequest.GetResponse();
_objSR = new StreamReader(_objResponse.GetResponseStream(), Encoding.ASCII);
_strContent = _objSR.ReadToEnd();
_contentLength = _strContent.Length;
_objSR.Close();
_objResponse.Close();

You can then parse the response as needed for your consumer application.

No comments:

Post a Comment