我在寫一些代碼時遇到了Internet /安全問題。在c#中啓動webRequest時發生SocketException#
基本上我試圖從c#代碼啓動一個HttpWebRequest到一個特定的URL。沒有什麼奇特的,只是一個基本的要求。
當我在我的本地機器上執行它工作正常。
當我將代碼部署到GetResponse的方法例外如下一個Windows Server 2003環境......
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()
有趣的是,如果我把相同的URL到Internet Explorer瀏覽器,頁面加載罰款等等我不認爲它的連接問題。
我想過也許防火牆? - >但它關掉了。
我也在調查.NET運行時安全並暫時將All_Code組設置爲FullTrust。
最後,我將URL添加到可信站點組。
不知道!!
任何人都可以幫忙嗎?
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postURL);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.GetResponse();
下面是跟蹤日誌...
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)
您可以從Windows 2003環境瀏覽到IE中的網址嗎? – tdaines 2011-03-19 05:24:22
@tdaines是的,正如我上面提到的。 IE顯示頁面沒有任何警告或問題。 – Grant 2011-03-19 05:26:12