Hello All,
before some days I was working with the .aspx pages and got in the situation that I needed the source of the page from code.
I am posting this small code snippet might be useful for others also
(Note:- There might be some issue while getting html source from the web page due to some network settings like proxies and secure sockets.)
//this creates a request to a web page.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url of webpage");
//use this for obtaining settings of network
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
Encoding encoding = Encoding.GetEncoding(responce.CharacterSet);
//gets responce in the format of stream
Stream stream = responce.GetResponseStream();
//use StreamReader to read the stream contents
StreamReader reader = new StreamReader(stream, encoding);
string content = reader.ReadToEnd();
then you can use this obtained string content for any purpose like writing to a file.
Please suggest me If you find some improvements to be done in this.
No comments:
Post a Comment