VB.NET中IIS下GetRequestStream函数失效问题求助
连接超时问题排查(IIS部署后失败,IIS Express/Postman正常)
环境与现象
- 运行环境:企业VPN内的Windows虚拟机,VB.NET代码调用
GetRequestStream发起POST请求 - 异常表现:Visual Studio的IIS Express环境、Postman发起相同请求均正常;部署到IIS后出现连接超时(Socket错误码10060)
相关代码
api.Method = "POST" api.ContentType = "application/json" api.ClientCertificates.Add(x509Certificate) 'correct certificate added here Dim byteArray As Byte() = Nothing api.ContentLength = 0 Try If objectInput IsNot Nothing Then Dim data As String = JsonConvert.SerializeObject(objectInput) 'objectInput has valid requestbody byteArray = Encoding.UTF8.GetBytes(data) api.ContentLength = byteArray.Length Using dataStream As IO.Stream = api.GetRequestStream() dataStream.Write(byteArray, 0, byteArray.Length) End Using End If Catch ex As Exception textToAppend = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " error in streamdata write " + JsonConvert.SerializeObject(api) File.AppendAllText(filePathLogs, textToAppend & Environment.NewLine) textToAppend = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " error in streamdata write " + JsonConvert.SerializeObject(ex) File.AppendAllText(filePathLogs, textToAppend & Environment.NewLine) End Try
日志记录的API信息
{ "_HttpRequestHeaders":["Content-Type","Host"], "_Proxy":{ "Credentials":null }, "_KeepAlive":true, "_Pipelined":false, "_AllowAutoRedirect":true, "_AllowWriteStreamBuffering":true, "_HttpWriteMode":1, "_MaximumAllowedRedirections":50, "_AutoRedirects":0, "_Timeout":100000, "_ReadWriteTimeout":300000, "_MaximumResponseHeadersLength":64, "_ContentLength":643, "_MediaType":null, "_OriginVerb":{}, "_ConnectionGroupName":null, "_Version":{ "Major":1, "Minor":1, "Build":-1, "Revision":-1, "MajorRevision":-1, "MinorRevision":-1 }, "_OriginUri":"https://svcproxy-csc-itg.it.hpe.com/gw/hpit/sapgts/gtslicense/1.0" }
异常信息
{ "ClassName":"System.Net.WebException", "Message":"Unable to connect to the remote server", "Data":null, "InnerException":{ "NativeErrorCode":10060, "ClassName":"System.Net.Sockets.SocketException", "Message":"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", "Data":null, "InnerException":null, "HelpURL":null, "StackTraceString":" at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)\r\n at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)", "RemoteStackTraceString":null, "RemoteStackIndex":0, "ExceptionMethod":"8\nDoConnect\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.Sockets.Socket\nVoid DoConnect(System.Net.EndPoint, System.Net.SocketAddress)", "HResult":-2147467259, "Source":"System", "WatsonBuckets":null }, "HelpURL":null, "StackTraceString":" at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)\r\n at System.Net.HttpWebRequest.GetRequestStream()\r\n at TechPro.download_content.API_CALL(HttpWebRequest api, String method, String ContentType, Object objectInput, X509Certificate2 x509Certificate, String referenceNumber, Boolean isStatusCheck) in C:\\Users\\sumitm\\Documents\\TechPro\\Source Code\\techpro\\TechPro\\download_content.aspx.vb:line 390", "RemoteStackTraceString":null, "RemoteStackIndex":0, "ExceptionMethod":"8\nGetRequestStream\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.HttpWebRequest\nSystem.IO.Stream GetRequestStream(System.Net.TransportContext ByRef)", "HResult":-2146233079, "Source":"System", "WatsonBuckets":null }
排查方向建议
- 代理配置差异:IIS Express使用当前登录用户的代理设置,而IIS应用池默认使用
ApplicationPoolIdentity等系统账户,该账户可能无VPN代理权限或未配置代理。可在代码中显式设置代理:api.Proxy = New WebProxy("http://your-proxy-address:port") api.Proxy.Credentials = CredentialCache.DefaultCredentials - 防火墙/网络策略:检查虚拟机防火墙是否允许出站访问目标地址
svcproxy-csc-itg.it.hpe.com的443端口;确认企业VPN对IIS应用池账户的网络访问权限。 - 证书权限:IIS应用池账户可能无客户端证书私钥读取权限。找到证书存储位置,右键证书→所有任务→管理私钥,添加应用池账户并赋予读取权限。
- DNS解析问题:在IIS服务器执行
nslookup svcproxy-csc-itg.it.hpe.com,确认能正常解析目标IP;若解析失败,检查DNS配置或手动添加hosts映射。 - TLS版本兼容性:目标服务器可能仅支持TLS 1.2+,而.NET运行环境默认未启用对应版本,可在代码中添加:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls13
内容的提问来源于stack exchange,提问作者Sumit Mandlik
相关产品推荐
相关产品推荐

