2011-03-19 2 views
1

J'ai un problème de sécurité Internet avec du code que j'ai écrit.SocketException lors de l'initialisation d'un webRequest dans C#

Fondamentalement, j'essaie d'initier un HttpWebRequest du code C# à une URL particulière. rien d'extraordinaire, juste une demande de base. Lorsque j'exécute sur ma machine locale, cela fonctionne très bien.

Quand je Déployez le code à un environnement serveur 2003 de Windows les exceptions de la méthode GetResponse comme suit ...

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 xxx.xxx.xxx.xxx:80 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
    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 xxxxxx.xxxxxxxxxx.xxxxxxxx.xxxxxxxxxx() 

Fait intéressant, si je mets la même URL dans un navigateur Internet Explorer, la page se charge très bien ainsi Je ne pense pas que c'est un problème de connectivité.

J'ai pensé peut-être un pare-feu? -> mais c'est éteint.

Je vérifie également la sécurité d'exécution de .NET et définit temporairement le groupe All_Code pour avoir FullTrust.

Enfin, j'ai ajouté l'URL au groupe de sites de confiance.

Aucune idée !!

Quelqu'un peut-il aider?

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postURL); 
httpRequest.Method = "POST"; 
httpRequest.ContentType = "application/x-www-form-urlencoded"; 
httpRequest.GetResponse(); 

Voici le journal de trace ...

System.Net Verbose: 0 : [5004] WebRequest::Create(http://xxxxxxxxxxxxxx) System.Net Verbose: 0 : [5004] HttpWebRequest#63835064::HttpWebRequest(http://xxxxxxxxxx#-1903155940) System.Net Verbose: 0 : [5004] Exiting HttpWebRequest#63835064::HttpWebRequest() System.Net Verbose: 0 : [5004] Exiting WebRequest::Create() -> HttpWebRequest#63835064 System.Net Verbose: 0 : [5004] HttpWebRequest#63835064::GetResponse() System.Net Information: 0 : [5004] Associating HttpWebRequest#63835064 with ServicePoint#11454272 System.Net Information: 0 : [5004] Associating Connection#60504909 with HttpWebRequest#63835064 System.Net.Sockets Verbose: 0 : [5004] Socket#21950498::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [5004] Exiting Socket#21950498::Socket() System.Net.Sockets Verbose: 0 : [5004] Socket#21950498::Connect(53:80#904160388) System.Net.Sockets Verbose: 0 : [5004] Exiting Socket#21950498::Connect() System.Net Information: 0 : [5004] Associating HttpWebRequest#63835064 with ConnectStream#37614147 System.Net Information: 0 : [5004] HttpWebRequest#63835064 - Request: POST /xxxxxxxxxxx HTTP/1.1

System.Net.Sockets Verbose: 0 : [5004] Socket#21950498::Send() System.Net.Sockets Verbose: 0 : [5004] Data from Socket#21950498::Send

// SOCKET DATA

System.Net.Sockets Verbose: 0 : [5004] Exiting Socket#21950498::Send() -> 297#297 System.Net Information: 0 : [5004] ConnectStream#37614147 - Sending headers { Content-Type: application/x-www-form-urlencoded Host: paint.pure360.com Connection: Keep-Alive }. System.Net.Sockets Verbose: 0 : [5004] Socket#21950498::Receive() System.Net.Sockets Verbose: 0 : [5004] Data from Socket#21950498::Receive

// SOCKET DATA

System.Net.Sockets Verbose: 0 : [5004] Exiting Socket#21950498::Receive() -> 176#176 System.Net Information: 0 : [5004] Connection#60504909 - Received status line: Version=1.1, StatusCode=302, StatusDescription=Found. System.Net Information: 0 : [5004] Connection#60504909 - Received headers { Connection: close Content-Length: 0 Content-Type: text/html Date: Wed, 23 Mar 2011 00:59:11 GMT Location: http://xxxxxxxxx Server: Apache }. System.Net Information: 0 : [5004] ConnectStream#35460724::ConnectStream(Buffered 0 bytes.) System.Net.Sockets Verbose: 0 : [5004] Socket#21950498::Dispose() System.Net Information: 0 : [5004] Associating HttpWebRequest#63835064 with ConnectStream#35460724 System.Net Information: 0 : [5004] Associating HttpWebRequest#63835064 with HttpWebResponse#65961401 System.Net Warning: 0 : [5004] HttpWebRequest#63835064::() - Error code 302 was received from server response. System.Net Warning: 0 : [5004] HttpWebRequest#63835064::() - Resubmitting request. System.Net Information: 0 : [5004] Associating HttpWebRequest#63835064 with ServicePoint#47602007 System.Net Information: 0 : [5004] Associating Connection#3927761 with HttpWebRequest#63835064 System.Net.Sockets Verbose: 0 : [5004] Socket#66771947::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [5004] Exiting Socket#66771947::Socket() System.Net.Sockets Verbose: 0 : [5004] Socket#66771947::Connect(22:80#383746063) System.Net.Sockets Error: 0 : [5004] Exception in the Socket#66771947::Connect - 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 95.128.223.22:80 System.Net.Sockets Verbose: 0 : [5004] Socket#66771947::Dispose() System.Net Error: 0 : [5004] Exception in the HttpWebRequest#63835064:: - Unable to connect to the remote server System.Net Error: 0 : [5004] at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest) System.Net Error: 0 : [5004] Exception in the HttpWebRequest#63835064::EndGetResponse - Unable to connect to the remote server System.Net Error: 0 : [5004]
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)

+0

Pouvez-vous parcourir l'URL dans IE à partir de l'environnement Windows 2003? – tdaines

+0

@tdaines oui comme je l'ai mentionné ci-dessus. IE montre la page bien sans avertissements ou problèmes. – Grant

Répondre

2

Votre configuration Win2003 a probablement un proxy par défaut qui est à l'origine de ce problème. Allez dans InternetOptions de votre installation IE et voir si un proxy est utilisé. En outre, donner un extrait du code aidera à mieux comprendre le problème.

+0

salut @feroze, ont vérifié l'existence d'un proxy et il n'y en a pas. En outre, il s'agit d'une application winforms -> n'est pas un journal de trace pour une application asp.net? – Grant

+0

Le fichier de configuration devrait fonctionner pour n'importe quelle application. Renommez-le simplement en app.exe.config où app.exe est votre nom de fichier exécutable. – feroze

+0

hi @feroze J'ai joint les informations de trace. est-ce que cela aide? – Grant

0

sur votre serveur Dépend vous tentez d'accéder.

Essayez fournir un agent utilisateur:

httpRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"; 
+0

hmmm. mais comme je l'ai dit, le code fonctionne bien sur mon local .. cela signifie-t-il que l'agent utilisateur n'est pas pertinent? – Grant