I am trying to parse a website that requires you to be logged in to access (NOT https). I have logged in via IE and told it to remember my credentials. If I use an invisible webBrowser control it will correctly log in and I can get the html source code from that. The problem is I need to be able to do this X number of times concurrently in BackgroundWorke rs, and it won’t let me create a webBrowser control dynamically in code due to some ActiveX error.
So, I figure I should use HttpWebRequest or WebClient to connect and get the html source. (In the end I’m going to be passing it to HtmlAgilityPack , but I don’t think that supports authentication either.)
Here is some code I have tried:
However it only returns the login page, not the actual page I was looking for. I also did the equivalent using WebClient and it fails as well. Any tips?
So, I figure I should use HttpWebRequest or WebClient to connect and get the html source. (In the end I’m going to be passing it to HtmlAgilityPack , but I don’t think that supports authentication either.)
Here is some code I have tried:
Code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.UseDefaultCredentials = true;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.PreAuthenticate = true;
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch
{
return "ERROR: Couldn't get response";
}
Stream resStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(resStream);
string temp = streamReader.ReadToEnd();
resStream.Close();
return temp;
Comment