
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Obtaining the HTML from a URL
|
803
proxyInfo.Address = new Uri(proxyUri);
// Tell it to bypass the proxy server for local addresses.
proxyInfo.BypassProxyOnLocal = true;
// Add any credential information to present to the proxy server.
proxyInfo.Credentials = new NetworkCredential(proxyID,
proxyPwd,
proxyDomain);
// Assign the proxy information to the request.
httpRequest.Proxy = proxyInfo;
}
// Return the request.
return httpRequest;
}
If all requests are going to go through the same proxy, in the 1.x versions of the
Framework, you used the static Select method on the GlobalProxySelection class to
set up the proxy settings for all
WebRequests. In Version 2.0, the WebRequest.Default-
WebProxy
property should be used:
Uri proxyURI = new Uri("http://webproxy:80");
WebRequest.DefaultWebProxy = new WebProxy(proxyURI);
// Old v1.x way of doing this...
//GlobalProxySelection.Select = new WebProxy(proxyURI);
Discussion
AddProxyInfoToRequest takes the URI of the proxy and creates a Uri object, which is
used to construct the
WebProxy object. The WebProxy object is set to bypass the proxy
for local addresses and then the credential information is used to create a
NetworkCredential object. The NetworkCredential object represents the authentica-
tion information necessary for the request to succeed ...