This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

Grabbing a remote web page in C#

Want to quickly grab a remote web page somewhere using C#? The code snippet below sends a get request to the URL you specify and returns a string containing the resulting page. If any error occurs it will return an empty string. You can of course change the catch block to throw any exceptions up to the calling code.


using System.Net;
using System.Text

private string GetWebPage(string url)
{
try
{
HttpWebRequest? webRequest =
(HttpWebRequest)WebRequest.Create(url);
webRequest.Timeout = 6000;
HttpWebResponse? webResponse =
(HttpWebResponse)webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
string responseEncoding = webResponse.ContentEncoding.Trim();
if (responseEncoding.Length == 0)
responseEncoding="us-ascii";
StreamReader? responseReader = new StreamReader(responseStream,
System.Text.Encoding.GetEncoding(responseEncoding));
return(responseReader.ReadToEnd());
}
catch
{
return(string.Empty);
}
}

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger