在c#中启动webRequest时出现SocketException
我编写的一些代码遇到了互联网/安全问题。
基本上我试图从 C# 代码启动 HttpWebRequest 到特定的 URL。没什么特别的,只是一个基本的要求。
当我在本地计算机上执行时,它工作正常。
当我将代码部署到 Windows Server 2003 环境时,GetResponse 方法出现如下异常...
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 详细信息:0:[5004] WebRequest::创建(http://xxxxxxxxxxxxx) System.Net 详细:0:[5004] HttpWebRequest#63835064::HttpWebRequest(http://xxxxxxxxxx#-1903155940) System.Net 详细:0:[5004] 正在退出 HttpWebRequest#63835064::HttpWebRequest() System.Net 详细:0:[5004] 正在退出 WebRequest::Create() -> HttpWebRequest#63835064 System.Net 详细:0:[5004] HttpWebRequest#63835064::GetResponse() 系统.Net信息:0:[5004] 关联 HttpWebRequest#63835064 使用 ServicePoint#11454272 System.Net 信息: 0 : [5004] 关联 连接#60504909 与 HttpWebRequest#63835064 System.Net.Sockets 详细:0:[5004] 套接字#21950498::套接字(InterNetwork#2) System.Net.Sockets 详细:0:[5004] 退出套接字#21950498::Socket() System.Net.Sockets 详细:0:[5004] 套接字#21950498::连接(53:80#904160388) System.Net.Sockets 详细:0:[5004] 退出套接字#21950498::Connect() 系统.Net信息:0:[5004] 关联 HttpWebRequest#63835064 使用 ConnectStream#37614147 System.Net 信息:0:[5004] HttpWebRequest#63835064 - 请求: POST /xxxxxxxxxxx HTTP/1.1
System.Net.Sockets 详细:0:[5004] 套接字#21950498::发送() System.Net.Sockets 详细:0:[5004] 来自 Socket#21950498::Send 的数据
// 套接字数据
System.Net.Sockets 详细:0:[5004] 退出 Socket#21950498::Send() -> 297#297 系统.Net 信息: 0 : [5004] 连接流#37614147 - 发送标头 { 内容类型: 应用程序/x-www-form-urlencoded 主机:paint.pure360.com 连接: 保持活动状态}。系统.网络.套接字 详细:0:[5004] 套接字#21950498::接收() System.Net.Sockets 详细:0:[5004] 来自 Socket#21950498::Receive 的数据
// 套接字数据
System.Net.Sockets 详细:0:[5004] 退出 Socket#21950498::Receive() -> 176#176 系统.Net 信息: 0 : [5004] 连接#60504909 - 已收到 状态行:版本=1.1, 状态代码=302, 状态描述=找到。系统网 信息:0:[5004] 连接#60504909 - 收到标头 { 连接:关闭 内容长度:0 内容类型:text/html 日期:23 日,星期三 2011 年 3 月 00:59:11 GMT 地点: http://xxxxxxxxx 服务器:Apache }。 系统.Net信息:0:[5004] ConnectStream#35460724::ConnectStream(缓冲 0 字节。)System.Net.Sockets 详细信息: 0 : [5004] 套接字#21950498::Dispose() 系统.Net信息:0:[5004] 关联 HttpWebRequest#63835064 使用 ConnectStream#35460724 System.Net 信息: 0 : [5004] 关联 HttpWebRequest#63835064 与 HttpWebResponse#65961401 System.Net 警告:0:[5004] HttpWebRequest#63835064::() - 错误 从服务器收到代码 302 回复。 System.Net警告:0: [5004] HttpWebRequest#63835064::() - 重新提交请求。系统网 信息: 0 : [5004] 关联 HttpWebRequest#63835064 与 ServicePoint#47602007 System.Net 信息: 0 : [5004] 关联 连接#3927761 与 HttpWebRequest#63835064 System.Net.Sockets 详细:0:[5004] 套接字#66771947::套接字(InterNetwork#2) System.Net.Sockets 详细:0:[5004] 退出套接字#66771947::Socket() System.Net.Sockets 详细:0:[5004] 套接字#66771947::连接(22:80#383746063) System.Net.Sockets 错误:0:[5004] 例外情况在 套接字#66771947::连接 - A 连接尝试失败,因为 关联方未妥善 一段时间后回复,或 建立的连接失败,因为 连接的主机未能响应 95.128.223.22:80 System.Net.Sockets 详细:0:[5004] Socket#66771947::Dispose() System.Net 错误:0:[5004] 中出现异常 HttpWebRequest#63835064:: - 无法 连接到远程服务器 System.Net 错误:0:[5004] 在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream、对象所有者、布尔值 异步,IP地址&地址、套接字和 abortSocket、Socket&中止Socket6, Int32 超时)于 System.Net.PooledStream.Activate(对象 owningObject、布尔异步、Int32 超时,GeneralAsyncDelegate 异步回调)在 System.Net.Connection.CompleteStartConnection(布尔值 异步,HttpWebRequest httpWebRequest) System.Net 错误:0:[5004] 异常 在 HttpWebRequest#63835064::EndGetResponse - 无法连接到远程服务器 System.Net 错误:0:[5004]
在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream、对象所有者、布尔值 异步,IP地址&地址、套接字和 abortSocket、Socket&中止Socket6, Int32 超时)于 System.Net.PooledStream.Activate(对象 owningObject、布尔异步、Int32 超时,GeneralAsyncDelegate 异步回调)在 System.Net.Connection.CompleteStartConnection(布尔值 异步,HttpWebRequest httpWebRequest)
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.1System.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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 Win2003 配置可能有一个默认代理导致此问题。进入 IE 安装的 InternetOptions 并查看是否正在使用代理。此外,给出代码片段将有助于更好地理解该问题。
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: