Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The remote server returned an error: (407) Proxy Authentication Required

I referred several websites, which had answer for this question "The remote server returned an error: (407) Proxy Authentication Required." ,but none were helpful. I wrote a sample code to check the proxy authentication in office. The code throws exception.

My requirement:- Verify what the website returns. Outside office, the code works fine, but in office it throws an exception due to proxy. When I hardcode the credentials using new NetworkCredential, it works fine.

int ResponseCode;
string url = "http://www.msftncsi.com/ncsi.txt";
WebRequest request = WebRequest.Create(url);

request.Credentials = CredentialCache.DefaultCredentials;
using (WebResponse response = request.GetResponse())
 {

   Stream dataStream = response.GetResponseStream();
   StreamReader reader = new StreamReader(dataStream);
   responseFromServer = reader.ReadToEnd();
   ResponseCode = (int)((HttpWebResponse)response).StatusCode;
   reader.Close();
}

I do not want to Hardcode. I referred the solution in http://social.msdn.microsoft.com/Forums/is/csharpgeneral/thread/c06d3032-dceb-4a1a-bb6a-778fd13a938a, but even that didnt help. What am I missing?

like image 577
user1473039 Avatar asked Dec 13 '22 01:12

user1473039


1 Answers

I had the same issue, this did the trick for me

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

like image 120
Javi Avatar answered Jan 21 '23 11:01

Javi