WebRequest.GetResponse间歇性超时

时间:2016-09-14 11:29:02

标签: vb.net webrequest getresponse

我正在使用一些继承的代码来访问交易API并在.GetResponse调用中获得间歇性超时。

由于它间歇性我确实认为它的资源相关 - 其​​他文章提到关闭一次性对象的重要性,所以我在.GetResponse周围使用了一个Using块。我是否处理过所有一次性用品?

我对Web / Http电话很陌生,所以请帮忙!

    Dim success As Boolean = False

    Do Until success = True
        Try
            Dim request = WebRequest.Create("https://poloniex.com/tradingApi")
            Dim nonce As Long = CLng((DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds) + nonceoffset
            Dim postData = [String].Format("command={0}&nonce={1}", method, nonce)
            postData = postData & params

            Dim hmAcSha = New HMACSHA512(Encoding.ASCII.GetBytes(PrivateKey))
            Dim messagebyte = Encoding.ASCII.GetBytes(postData)
            Dim hashmessage = hmAcSha.ComputeHash(messagebyte)
            Dim sign = BitConverter.ToString(hashmessage)
            sign = sign.Replace("-", "")

            request.Timeout = 5000
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = messagebyte.Length
            request.Method = "POST"
            request.Headers.Add("Key", PublicKey)
            request.Headers.Add("Sign", sign.ToLower())

            System.Net.ServicePointManager.Expect100Continue = False

            Dim stream As System.IO.Stream
            stream = request.GetRequestStream()
            stream.Write(messagebyte, 0, messagebyte.Length)
            Dim json As String
            Using response As WebResponse = request.GetResponse()
                Dim postreqreader = New StreamReader(response.GetResponseStream())
                json = postreqreader.ReadToEnd()
            End Using

            success = True
            Return json

        Catch e As WebException
            DebugTextAdd("ALERT! API WebException : " & e.Message, MethodBase.GetCurrentMethod.Name())
            If System.Threading.Interlocked.Decrement(maxRetryCount) = 0 Then
                DebugTextAdd("ALERT! API call failed after several tries. Return Nothing", MethodBase.GetCurrentMethod.Name())
                success = True
                Return Nothing
            Else
                DebugTextAdd("Attempt #" & maxRetryCount.ToString & ", try again", MethodBase.GetCurrentMethod.Name())
            End If

        Catch e As Exception
            DebugTextAdd("ALERT! API exception : " & e.Message, MethodBase.GetCurrentMethod.Name())
            If System.Threading.Interlocked.Decrement(maxRetryCount) = 0 Then
                DebugTextAdd("ALERT! API call failed after several tries. Return Nothing", MethodBase.GetCurrentMethod.Name())
                success = True
                Return Nothing
            Else
                DebugTextAdd("Attempt #" & maxRetryCount.ToString & ", try again", MethodBase.GetCurrentMethod.Name())
            End If

        Finally

        End Try
    Loop

0 个答案:

没有答案