Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HTTP Read w/ Authentication

HTTP Read w/ Authentication

Scheduled Pinned Locked Moved C#
questionhtmlsecurityhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larry Rutledge
    wrote on last edited by
    #1

    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?

    L B 2 Replies Last reply
    0
    • L Larry Rutledge

      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?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Passing http://user:pass@www.website.com/ should work. :) WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.

      1 Reply Last reply
      0
      • L Larry Rutledge

        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?

        B Offline
        B Offline
        Burt Harris
        wrote on last edited by
        #3

        Authentication information is handled via the HttpWebRequest.Credentials property. You can set this to either an instance of NetworkCredential or CredentialCache. 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups