Capture response from server

If you need to get the response from a server without displaying it on the browser, use the HttpWebRequest and HttpWebResponse classes. Sample code is below. If you want to post data and capture the response, click
post data to server and capture response.

StringBuilder str = new StringBuilder();
byte[] buffer = new byte[8000];

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
"http://www.siteurl.com"
);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream stream = response.GetResponseStream();

string temp = null;
int count = 0;

do
{
count = stream.Read(buffer, 0, buffer.Length);
if(count != 0)
{
temp = Encoding.ASCII.GetString(buffer, 0, count);
str.Append(temp);
}
}
while(count > 0);

Advertisements

One thought on “Capture response from server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s