Come di rilevare automaticamente le impostazioni proxy / Utilizzo di IE in .net HttpWebRequest

StackOverflow https://stackoverflow.com/questions/1155363

Domanda

E 'possibile rilevare / riutilizzare queste impostazioni?

Come?

L'eccezione sto ottenendo è Questa è l'eccezione durante la connessione al http://www.google.com

System.Net.WebException: Unable to connect to the remote server --->
  System.Net.Sockets.SocketException: A connection attempt failed because the
  connected party did not properly respond after a period of time, or established
  connection failed because connected host has failed to respond 66.102.1.99:80

  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
     SocketAddress socketAddress)
  at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
     Socket s4, Socket s6, Socket& socket, IPAddress& address,
     ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
     Exception& exception)
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.GetResponse()
  at mvcTest.MvcApplication.Application_Start() in
     C:\\home\\test\\Application1\\Application1\\Program.cs:line 33"
È stato utile?

Soluzione

HttpWebRequest effettivamente utilizzare le impostazioni del proxy IE per impostazione predefinita.

Se non desidera utilizzarli, è necessario eseguire l'override specifico la proprty .Proxy a uno nullo (nessun proxy), o le impostazioni del proxy di voi scelta.

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://news.bbc.co.uk");
 //request.Proxy = null; // uncomment this to bypass the default (IE) proxy settings
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 Console.WriteLine("Done - press return");
 Console.ReadLine();

Altri suggerimenti

mi è stato sempre una situazione molto simile dove il HttpWebRequest non stava raccogliendo i dati corretti del proxy per impostazione predefinita e l'impostazione delle useDefaultCredentials non ha funzionato neanche. Forzare le impostazioni nel codice tuttavia ha funzionato a meraviglia:

IWebProxy proxy = myWebRequest.Proxy;
if (proxy != null) {
    string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString;
    myWebRequest.UseDefaultCredentials = true;
    myWebRequest.Proxy = new WebProxy(proxyuri, false);
    myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}

e per questo utilizza le credenziali di default non dovrebbe chiedere all'utente per i loro dettagli.

Si noti che questo è un duplicato della mia risposta postato qui per un problema molto simile: Proxy autenticazione di base in C #: HTTP 407 errore

Per le persone che hanno problemi con ottenere questo di bel gioco con server ISA, si potrebbe tentare di configurare proxy nel seguente modo:

IWebProxy webProxy = WebRequest.DefaultWebProxy;
webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
myRequest.Proxy = webProxy;

Questo accade per difetto, se WebRequest.Proxy non è impostato in modo esplicito (di default è impostato su WebRequest.DefaultWebProxy).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top