开发者

SocketException when initiating a webRequest in c#

开发者 https://www.devze.com 2023-02-17 23:59 出处:网络
i am having a internet / security issue with some code i have written. Basically i am trying to initiate a HttpWebRequest from c# code to a particular URL. nothing fancy, just a basic request.

i am having a internet / security issue with some code i have written.

Basically i am trying to initiate a HttpWebRequest from c# code to a particular URL. nothing fancy, just a basic request.

When i execute on my local machine it works fine.

When i deploy the code to a Windows server 2003 environment the GetResponse method exceptions as follows...

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

Interestingly, if i put the same URL into an internet explorer browser, the page loads fine so i dont think its a connectivity issue.

I have thought maybe firewall? -> but its turned off.

I also investigation .NET runtime security and temporarily set the All_Code group to have FullTrust.

Lastly, i added the URL to the trusted sites group.

No idea!!

Can anyone help?

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

Here is the trace log...

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#21开发者_C百科950498::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)


Your Win2003 configuration probably has a default proxy that is causing this problem. Go into InternetOptions of your IE installation and see if there is a proxy being used. Also, giveing a snippet of the code will help understand the issue more.


Depends on you server you're trying to access.

Try supplying a user agent:

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

精彩评论

暂无评论...
验证码 换一张
取 消