Ever try to call a web page from client site .NET code? Most often this is doen through web services, but occationally you have information that you want to retrieve that does not exists in a web service -- only in the web page. So from .NET, how do you get it?
Like this:
private string RetrieveWebPage(string url)
{
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url);
// if you are dealing with NTLM, you will need this line
oRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
Stream oStream = oResponse.GetResponseStream();
// read the stream in the encoded "utf-8" format
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader oReader = new StreamReader(oStream);
string sPage = "";
while (oReader.Peek() > -1)
{
sPage += oReader.ReadLine() + "\n";
}
return sPage;
}
No comments:
Post a Comment