كيفية الاستفادة من أو الاستخدام / استخدام إعدادات الوكيل في .NET HttpWebRequest

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

سؤال

هل من الممكن اكتشاف / إعادة استخدام تلك الإعدادات؟

كيف ؟

الاستثناء الذي أحصل عليه هو الاستثناء أثناء الاتصال 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"
هل كانت مفيدة؟

المحلول

سيستخدم HttpWebRequest إعدادات الوكيل IE افتراضيا.

اذا أنت لا تريد استخدامها، يجب عليك تجاوز .Proxy Proprty إما NULL (بدون وكيل)، أو إعدادات الوكيل لك.

 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();

نصائح أخرى

كنت أحصل على وضع متشابه للغاية حيث لم يكن HttpWebRequest يلتقط تفاصيل الوكيل الصحيحة بشكل افتراضي ولم يعمل إعداد epledefaultcredentials أيضا. فغم الإعدادات في التعليمات البرمجية ومع ذلك عملت علاج:

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;
}

ولأن هذا يستخدم بيانات الاعتماد الافتراضية، يجب ألا تطلب من المستخدم عن تفاصيلهم.

لاحظ أن هذا مكررة من إجابتي المنشورة هنا لمشكلة مماثلة للغاية: المصادقة الأساسية الوكيل في C #: HTTP 407

بالنسبة للأشخاص الذين يواجهون مشاكل في الحصول على هذا للعب لطيف مع ISA Server، قد تحاول إعداد وكيل بالطريقة التالية:

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

يحدث هذا بشكل افتراضي، إذا لم يتم تعيين WebRequest.Proxy بشكل صريح (افتراضيا، فهذا WebRequest.DefaultWebProxy).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top