For either kind of authentication you specify the

Info icon This preview shows pages 543–545. Sign up to view the full content.

View Full Document Right Arrow Icon
tication over HTTPS seems to be the popular choice. For either kind of authentication, you specify the username and password in the way shown in Example 13-17 . Example 13-17. Providing credentials for basic or digest authentication HttpWebRequest request = (HttpWebRequest) WebRequest.Create(" "); request.Credentials = new NetworkCredential("user1", "[email protected]"); ... This approach doesn’t let you specify whether to use basic or digest authentication because the server gets to choose. Since you therefore don’t know whether the password will be sent in the clear, you should normally provide credentials this way only when using HTTPS. You can force the use of digest authentication by wrapping the Network Credential in a CredentialCache object, which lets you specify the authentication schemes you want to support. Even so, you might want to be wary of using digest authentication without HTTPS—although digest authentication can be secure, some servers implement it in an unsecure way. HTTP | 519
Image of page 543

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Working with proxies By default, web requests will look at the Internet Explorer settings to determine whether a web proxy should be used. But you might not want this default behavior, so there are a couple of ways you can change it. Prior to .NET 2.0, IE proxy settings weren’t honored, so you may oc- casionally come across code that goes to some lengths to work out whether it needs to use a proxy. Usually such code is either old or written by someone who didn’t know that .NET 2.0 fixed this issue. You can add entries to your App.config file to modify the default proxy behavior. Example 13-18 stops web requests using the configured default proxy by default. Example 13-18. Configuring default proxy behavior <configuration> <system.net> <defaultProxy enabled="false" /> </system.net> </configuration> The default behavior, in the absence of any configuration, specifies that the use of the default proxy is enabled, but the application will not use the user’s credentials to iden- tify the user to the proxy server. (Authenticating the user to a proxy happens inde- pendently of authenticating the user to the web server.) Some companies require users to authenticate with the proxy in order to access the Internet, in which case you would need to change the configuration, setting the <defaultProxy> element’s useDefaultCre dentials attribute to true . You can also modify the behavior in code. The HttpWebRequest class has a Proxy prop- erty, and you can set this to null to disable the use of a proxy. Or you can set it to a WebProxy object specifying a specific proxy and settings, as Example 13-19 shows. Example 13-19. Setting an explicit proxy HttpWebRequest request = (HttpWebRequest) WebRequest.Create(" "); request.Proxy = new WebProxy(" "); Controlling cache behavior Windows maintains a per-user cache of web resources, to avoid having to download frequently used bitmaps, CSS, JavaScript, HTML pages, and other content again and again. Internet Explorer uses this cache, but it’s also accessible to .NET code. By default,
Image of page 544
Image of page 545
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern