代码适用于PhP,但不适用于VB

时间:2017-11-06 17:55:38

标签: php vb.net curl

我尝试执行一些访问livecoin API的代码。

使用fiddler我的帖子命令如果使用vb.net如下(我把我的API密钥但不是我的秘密,所以它是安全的)。

POST https://api.livecoin.net/exchange/cancellimit HTTP/1.1
Api-Key: 5Gs9BQ4RTggMPwWMwkzvCnGdvhcn6FfN
Sign: 5BA7FC2C46DC781FCCF37F78C0DB6E791BF3F7CFCA65B99C2E36A826B416A557
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0
Pragma: no-cache
Host: api.livecoin.net
Content-Length: 40
Expect: 100-continue
Connection: Keep-Alive

currencyPair=BTC%2FUSD&orderId=496303677

这不起作用。我收到了这个回复

{"success":true,"cancelled":false,"message":"Failed to cancelOrder[orderId={1}]|0[Unknown currency pair [currencyPair={1}]|null]"}

如果我使用curl

帖子消息是这个

POST https://api.livecoin.net/exchange/cancellimit HTTP/1.1
Host: api.livecoin.net
Accept: */*
Api-Key: 5Gs9BQ4RTggMPwWMwkzvCnGdvhcn6FfN
Sign: 5BA7FC2C46DC781FCCF37F78C0DB6E791BF3F7CFCA65B99C2E36A826B416A557
Content-Length: 40
Content-Type: application/x-www-form-urlencoded

currencyPair=BTC%2FUSD&orderId=496303677

这个有效。

{"success":true,"cancelled":false,"message":"Order[orderId={1}] isn't in OrderBook: status={1}|496303677|CANCELLED"}

哪个是对的。应该是这样的。请注意,帖子内容完全相同。甚至签名都是一样的。

我想知道为什么几乎完全相同的post命令会产生两种不同的结果。

我想在制作中使用的是vb.net。

实际代码

Protected Overrides Function getJsonPrivate(method As String, otherParameters() As Tuple(Of String, String)) As String
    Dim Uri As String
    otherParameters = otherParameters.ToList.OrderBy(Function(x) x.Item1).ToArray

    Dim param = jsonHelper.createPostStringFromTupples(otherParameters) 'orderId=495852754&currencyPair=BTC/USD
    Uri = "https://api.livecoin.net/" + method '
    Dim HMACSHA256 = New HMACSHA256(Encoding.UTF8.GetBytes(_secret1))
    Dim sig = ExchangesClass.CalculateSignature2(param, HMACSHA256).ToUpper
    Dim headers = {Tuple.Create("Api-Key", _apiKey1), Tuple.Create(Of String, String)("Sign", sig)}

    Dim result = CookieAwareWebClient.downloadString1(Uri, param, headers)
    Return result
End Function

在大多数情况下,一个代码工作正常。

Public Shared Function downloadString1(url As String, post As String, otherHeaders As Tuple(Of String, String)()) As String
    Dim wc = New CookieAwareWebClient()
    For Each oh In otherHeaders
        wc.Headers.Add(oh.Item1, oh.Item2)
    Next

    Dim response = String.Empty

    Try
        If post = "" Then
            response = wc.DownloadString(url)
        Else
            response = wc.UploadString(url, post)
        End If
    Catch ex As Exception
        Dim a = 1
    End Try

    Return response
End Function

   Public Shared Function downloadString1(url As String, post As String, otherHeaders As Tuple(Of String, String)()) As String
        Dim wc = New CookieAwareWebClient()
        For Each oh In otherHeaders
            wc.Headers.Add(oh.Item1, oh.Item2)
        Next

        Dim response = String.Empty

        Try
            If post = "" Then
                response = wc.DownloadString(url)
            Else
                response = wc.UploadString(url, post)
            End If
        Catch ex As Exception
            Dim a = 1
        End Try

        Return response
    End Function

似乎问题是livecoin api解释currencyPair = BTC%2FUSD

的方式

但正如我所说,两者都有完全相同的帖子内容。

0 个答案:

没有答案