Beating Error 407 In SharePointPosted on Friday, 05 June 2009 08:11
If you don't want to put passwords into your web.config for the proxy, you can do so programatically and beat error 407 in SharePoint. Here's how.
You, like me, have probably struggled on numerous occasions to get a SharePoint webpart to connect to a different website / webservice, such as google api or twitter api, and failed because you came across the error: "The remote server returned an error: (407) Proxy Authentication Required".
I tried modifying the web.config but this didn't work, I tried adding the proxy with usedefaultcredentials = true, but this didn't work either.
If you've gone this far and nothing is working for you, this is how it's done.
Uri myUri = new Uri("http://www.mattknott.com");
WebRequest myWebRequest = WebRequest.Create(myUri);
WebProxy proxy = new WebProxy("http://webproxy.mydomain.com:8080", true);
System.Net.NetworkCredential credentials = new NetworkCredential("sharepoint.user", "password", "logon_domain");
proxy.UseDefaultCredentials = false;
proxy.Credentials = credentials;
myWebRequest.Proxy = proxy;
WebResponse myWebResponse = myWebRequest.GetResponse();
Instead of trying to use the credentials of the current user, an account is specified that allows access to the proxy.
I'd tried everything and this was the only thing that worked for me, I hope it helps you too.