Pregunta

I know this has been asked many many times. I have read most all posts on here and other sites like this one.

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/2931d21c-9ca8-4256-b213-915fad4c941b/

With no avail. Here's the environment

Windows Server 2008 R2 64bit Visual Studio 2008 .Net Framework 3.5

Here is what I have tried

I had the proxy authenticating using code

WebRequest req = WebRequest.Create(requestUri + data);
req.Proxy = new System.Net.WebProxy(<ProxyURL>:<port>",true);
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse resp = req.GetResponse();

This worked, but seeing as it was slowing down the app I learned that I can edit the machine.config file which I did. It worked too!

    <system.net>
      <defaultProxy
      useDefaultCredentials="true">
      <proxy
        proxyaddress="<proxyURL>:<port>"
        bypassonlocal="True"/>
     </defaultProxy>
   </system.net>

At least for a day or 2. Then it began failing.

I then edited it to this

    <system.net>
     <defaultProxy
       useDefaultCredentials="true">
       <proxy usesystemdefault="True"/>
       </defaultProxy>
    </system.net>

From my understanding this will use IE settings to connect to proxy but still does not work. I also tried tihs code

WebProxy proxy = new WebProxy(<proxy>:<port>);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(requestUri + data), "BASIC", new NetworkCredential(<username>,<password>));
proxy.Credentials = myCache;
request.Proxy = proxy;
request.Method = "GET";

And this did not work.

Note: I can copy the machine.config file to my computer(Win XP) and run the .exe(without the proxy code) from there and it works fine.

Is there something different I need to do with a 64 bit OS? Also I can open IE8 on the server and access the URI just fine. the goal is to preauthenticate the proxy without having to supply a username password in the code.

¿Fue útil?

Solución 2

HttpWebRequest uses the default Internet Settings (IE) proxy anyway, so if it works fine from Internet Explorer on the server, it should be ok from your code as well (provided it's running under the same user account).

I would put machine.config back as it was.

One thing I would check would be in IIS, is you can configure the Providers for the Windows Authentication applet. This should list NTLM and Kerberos as the providers in a list; I would switch them around and see if that makes a difference (e.g. if NTLM is top of the list, move Kerberos to the top). I'm sorry I can't give you the exact instructions as I don't have IIS on this machine.

If you're still struggling, I would recommend you run Fiddler on the server to capture the request and response flow for more clues.

Otros consejos

@David Moore is right. if IE is working fine when you browse manually then just add req.Proxy.Credentials = CredentialCache.DefaultCredentials; and it will work fine.

Heres a modified code taken from the MSDN which is working for me.

using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string urlDemo = "http://en.wikipedia.org/wiki/Main_Page";
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(urlDemo);
            // If required by the server, set the proxy credentials.
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            Console.ReadLine();
            // Clean up the streams and the response.
            reader.Close();
            response.Close();

        }
    }
}

hope it helps ;-)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top