我正在从TD-Ameritrade提取实时数据,并且在执行 getResponse()后几秒钟后,连接始终关闭。这是代码。
Try
Dim client As New clsTDABase
Dim values As NameValueCollection = New NameValueCollection From {{"source", Global_TDASourceID}}
Dim responseBody = client.local_TDAClient.UploadString("https://apis.tdameritrade.com/apps/100/StreamerInfo;jsessionid=" + client.local_TDASessionID + "?&source=" + Global_TDASourceID, "")
Dim streamerInfo = XMLConversionHelper.XMLToStreamerInfoObject(responseBody)
Dim sb As New StringBuilder
With streamerInfo
sb.Append("!U=" & 54321548).
Append("&W=" & .objToken).
Append("&A=" & 54321548).
Append("&token=" & .objToken).
Append("&company=AMER").
Append("&segment=AMER").
Append("&cddomain=" & .objCdDomainId).
Append("&usergroup=" & .objUserGroup).
Append("&accesslevel=" & .objAccessLevel).
Append("&authorized=" & .objAuthorized).
Append("&acl=" & .objACL).
Append("×tamp=" & .objTimeStamp).
Append("&appid=" & .objAppId).
Append("|")
End With
Dim info = sb.ToString + "S=QUOTE&C=MONOPOLIZE\n\n" '|S=QUOTE&C=SUBS&P=DELL+$SPX.X+$INX+$INDU&T=0+1+2+3+8+10+11+12+13+15+16+19+20\n\n"
Dim send As Byte()
send = Encoding.UTF8.GetBytes(WebUtility.UrlEncode(info))
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("https://ameritrade02.streamer.com/"), HttpWebRequest)
req.Method = "POST"
req.UserAgent = "Mozilla / 5.0(Windows NT 10; WOW64; Trident / 7.0; rv: 11.0) like Gecko"
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = send.Length
req.CookieContainer = New CookieContainer()
req.ServicePoint.MaxIdleTime = 60000
req.ServicePoint.Expect100Continue = False
req.Date = DateTime.Now
req.Timeout = Timeout.Infinite
req.ReadWriteTimeout = Timeout.Infinite
req.KeepAlive = True
req.AllowAutoRedirect = True
req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
req.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US;q=0.8,en-GB;q=0.5,en;q=0.3")
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8")
req.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
req.Headers.Add("DNT", "1")
Dim dataStream = req.GetRequestStream
dataStream.Write(send, 0, send.Length)
dataStream.Close()
Dim res = req.GetResponse()
Dim stream = res.GetResponseStream()
Debug.Print(Convert.ToChar(stream.ReadByte).ToString)
End Try
'外部功能
Public Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
Return True
End Function
附加的信息由StreamerInfo对象传播。该代码在代码底部附近的getResponse()处失败。
不管我做什么,连接都会在2-3秒后被杀死。