HTTP Read w/ Authentication
-
I used the following code to download the source of an HTML page:
... try { // _url is a private variable which contains the URL to download HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(_url); if (_useProxy) { WebProxy _proxy = new WebProxy(_proxyHost + ":" + _proxyPort.ToString()); _request.Proxy = _proxy; } HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); if (_response.StatusCode == HttpStatusCode.OK) { Stream _stream = _response.GetResponseStream(); StreamReader _streamReader = new StreamReader(_stream, Encoding.UTF8); MessageBox.Show(_streamReader.ReadToEnd().Trim()); } catch (WebException we) { // HTTP response was not "OK", handle HTTP error } catch (Exception e) { // Handle exception } ...
My question is, what if the page I need to access requires authentication. In other words, when browsing normally, with a web browser client, a dialog pops up asking for my username and password. I have the access information, but I want the preceding code to be able to send that authentication information along and get the code without any user intervention. Any ideas? -
I used the following code to download the source of an HTML page:
... try { // _url is a private variable which contains the URL to download HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(_url); if (_useProxy) { WebProxy _proxy = new WebProxy(_proxyHost + ":" + _proxyPort.ToString()); _request.Proxy = _proxy; } HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); if (_response.StatusCode == HttpStatusCode.OK) { Stream _stream = _response.GetResponseStream(); StreamReader _streamReader = new StreamReader(_stream, Encoding.UTF8); MessageBox.Show(_streamReader.ReadToEnd().Trim()); } catch (WebException we) { // HTTP response was not "OK", handle HTTP error } catch (Exception e) { // Handle exception } ...
My question is, what if the page I need to access requires authentication. In other words, when browsing normally, with a web browser client, a dialog pops up asking for my username and password. I have the access information, but I want the preceding code to be able to send that authentication information along and get the code without any user intervention. Any ideas? -
I used the following code to download the source of an HTML page:
... try { // _url is a private variable which contains the URL to download HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(_url); if (_useProxy) { WebProxy _proxy = new WebProxy(_proxyHost + ":" + _proxyPort.ToString()); _request.Proxy = _proxy; } HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); if (_response.StatusCode == HttpStatusCode.OK) { Stream _stream = _response.GetResponseStream(); StreamReader _streamReader = new StreamReader(_stream, Encoding.UTF8); MessageBox.Show(_streamReader.ReadToEnd().Trim()); } catch (WebException we) { // HTTP response was not "OK", handle HTTP error } catch (Exception e) { // Handle exception } ...
My question is, what if the page I need to access requires authentication. In other words, when browsing normally, with a web browser client, a dialog pops up asking for my username and password. I have the access information, but I want the preceding code to be able to send that authentication information along and get the code without any user intervention. Any ideas?Authentication information is handled via the
HttpWebRequest.Credentials
property. You can set this to either an instance ofNetworkCredential
orCredentialCache
. See the help under these two classes. If your using Windows authentication (typically only in a corproate intranet scenario) this works to provide the credentials the current use is logged in with:request.Credentials = System.Net.CredentialCache.DefaultCredentials;
Burt Harris