Se state lavorando in una macchina collegata ad una rete con un bel proxy server, e volete provare, chessò, a cercare di chiamare un web service, oppure semplicemente a caricare un file Xml remoto, con il metodo Load della classe XmlDocument (quindi una qualsiasi richiasta Http), vedrete apparirvi questo errore:

Error 407 - Proxy Authentication Required

La cosa più bella è che c'ha ragione lui, perchè prima di fare una qualunque richiesta http che cerca di uscire dalla vostra intranet, dovete auteticarvi presso il server proxy.
Eccome come farlo via codice:

C#
public Stream getStream()

{
WebResponse response;
WebProxy proxy = new WebProxy("http://serverProxy:porta");
WebRequest request = HttpWebRequest.Create("http://www.peppedotnet.it");
NetworkCredential myCred = new NetworkCredential("username", "password", "dominio");
proxy.Credentials = myCred;
request.Proxy = proxy;
response = request.GetResponse();
return response.GetResponseStream();
}

VB.NET
Public Function getStream() As Stream

Dim response As WebResponse
Dim proxy As New WebProxy("http://serverProxy:porta")
Dim request As WebRequest = HttpWebRequest.Create("http://www.peppedotnet.it")
Dim myCred As NetworkCredential = New NetworkCredential("username", "password", "dominio")
proxy.Credentials = myCred
request.Proxy = proxy
response = request.GetResponse()
Return response.GetResponseStream()
End Function
Commenti(2) - Posted @ 6/6/2005 2:38:22 PM - Categoria: .NET General - Permalink - Share on twitter | facebook


COMMENTI
Autore: Luiz Fernando - scritto il 7/15/2005 7:33:51 PM
Hi , I have to download a file.
I used the same code:
Dim W As New WebClient
Dim response As WebResponse
Dim proxy As New WebProxy("http://ProxyIP:Door")
Dim request As WebRequest = HttpWebRequest.Create("http://www.projetando.net") 'http://www.peppedotnet.it
Dim myCred As NetworkCredential = New NetworkCredential("user", "password")
proxy.Credentials = myCred
request.Proxy = proxy
response = request.GetResponse()
Dim str As Stream = response.GetResponseStream()

But when this code below run, I have 407 Proxy Authentication Requeried error.

W.DownloadFile("http://www.projetando.net", "C:\Retorno.zip")

Somebody help me, please??
Tks

Autore: Peppe - scritto il 7/15/2005 8:24:31 PM
Hi Luiz !
Well, my code is written for authenticate a web request to the web proxy on a local intranet and get out of that.
If you want to download a file from another server, you must have the required permission in that server ... if you haven't the authentication fails and returns 407 error.

INSERISCI UN COMMENTO

Nome *
Indirizzo e-mail
(non verrà pubblicato)
Commento *