Making web requests to external sites from SharePoint

In most enterprise settings you will need to go through a proxy server to connect to external sites. In Windows the proxy settings in IE actually affects the apps on that machine. If you write a console app to test your code for example, it will work fine and able to connect to the external site (provided proxy settings in your IE are correct).

Copy the same code to SharePoint and it stops working. This is because by default SharePoint is not configured to use the proxy settings in IE. To get this to work there are 2 options.

Option 1: Modify SharePoint’s web.config to specify the proxy

  • Edit the web.config of the SharePoint web app.
  • Search for the defaultProxy element. It will look like below
    <defaultProxy />
  • Change it to

Option 2: Set the proxy in code for the HttpWebRequest object

WebProxy proxy = new WebProxy("123.456.78.9", 1234);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Proxy = proxy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

If your proxy requires credentials you will get the exception: (407) Proxy Authentication Required. To fix this add these lines:

if (request.Proxy != null)
	request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

Update regarding the (407) Proxy Authentication Required exception:

Setting the proxy’s credentials as above worked for me on my dev machine, which was a single-server farm. It did not work when I deployed the code to a multi-servers farm environment, and I kept getting the (407) Proxy Authentication Required exception. I found that wrapping the code that makes the request in a SPSecurity.RunWithElevatedPrivileges block solved the issue. Can’t explain what’s going on, but it works now :).


About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in SharePoint. Bookmark the permalink.

2 Responses to Making web requests to external sites from SharePoint

  1. Pingback: Configure Proxy server for SharePoint - Keep it Simple

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s