Bitfinex API与VBnet

时间:2014-09-26 02:37:27

标签: json vb.net encoding

这是我到目前为止所提出的:

Sub GetData()
Try

        Dim method As String = calldata("/balances")
        MsgBox(method)

    Catch ex As Exception

    End Try
End Sub

Function calldata(ByVal Method As String) As String

    Dim logincookie As CookieContainer


    Try
        Dim pKey As String = "CODE HERE"
        Dim sKey As String = "SECRET CODE HERE"

        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://api.bitfinex.com/v1/"), HttpWebRequest)
        Dim randomn As String = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalSeconds)

        '//Dependant upon Method
        Dim postData As String = "method=" & Method & "&nonce=" & randomn
        Dim tempcookies As New CookieContainer

        '//Start Encryption
        Dim KeyByte() As Byte = Encoding.ASCII.GetBytes(sKey)
        Dim HMAcSha As New HMACSHA384(Encoding.ASCII.GetBytes(sKey))

        Dim messagebyte() As Byte = Encoding.ASCII.GetBytes(postData)
        Dim hashmessage() As Byte = HMAcSha.ComputeHash(messagebyte)
        Dim Sign As String = BitConverter.ToString(hashmessage)

        Sign = Sign.Replace("-", "")

        '//Generate Post Information
        postReq.Method = "POST"
        postReq.KeepAlive = False
        postReq.Headers.Add("X-BFX-APIKEY", pKey)
        postReq.Headers.Add("X-BFX-PAYLOAD")
        postReq.Headers.Add("X-BFX-SIGNATURE", LCase(Sign))
        postReq.CookieContainer = tempcookies

        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
        postReq.ContentLength = messagebyte.Length

        '//Send Request
        System.Net.ServicePointManager.Expect100Continue = False

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(messagebyte, 0, messagebyte.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse

        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
        tempcookies.Add(postresponse.Cookies)
        logincookie = tempcookies

        Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

        'The Response Text
        Dim thepage As String = postreqreader.ReadToEnd
        thepage = thepage.Replace(Chr(34), Chr(39))

        Return thepage

    Catch
        Return False

    End Try


End Function

我无法弄清楚有效载荷部分。此脚本是我用于其他API的修改版本。这是Bitfinex API信息

https://www.bitfinex.com/pages/api

除了有偿负担外,一切正常吗?我如何完成" payload = parameters-dictionary - > JSON编码 - > BASE64" API细节的一部分?

1 个答案:

答案 0 :(得分:0)

试试这个。

Sub GetData()

        Try
            Dim method As String = calldata("/balances")
            MsgBox(method)


        Catch ex As Exception

        End Try
End Sub


    Function calldata(ByVal Method As String) As String

        Dim logincookie As CookieContainer


        Try
            Dim pKey As String = "CODE HERE"
            Dim sKey As String = "SECRET CODE HERE"

            Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://api.bitfinex.com/v1/balances"), HttpWebRequest)
            Dim randomn As String = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalSeconds)

            '//Dependant upon Method
            Dim postData As String = "{""request"": ""/v1" & Method & """,""nonce"": """ & randomn & """,""options"":{}}"
            Dim tempcookies As New CookieContainer
            Dim payload As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(postData))

            '//Start Encryption
            Dim KeyByte() As Byte = Encoding.ASCII.GetBytes(sKey)
            Dim HMAcSha As New HMACSHA384(Encoding.ASCII.GetBytes(sKey))
            Dim messagebyte() As Byte = Encoding.ASCII.GetBytes(payload)
            Dim hashmessage() As Byte = HMAcSha.ComputeHash(messagebyte)
            Dim Sign As String = BitConverter.ToString(hashmessage)

            Sign = Sign.Replace("-", "")

            '//Generate Post Information
            postReq.Method = "POST"
            postReq.KeepAlive = False
            postReq.Headers.Add("X-BFX-APIKEY", pKey)
            postReq.Headers.Add("X-BFX-PAYLOAD", payload)
            postReq.Headers.Add("X-BFX-SIGNATURE", LCase(Sign))
            postReq.CookieContainer = tempcookies

            postReq.ContentType = "application/x-www-form-urlencoded"
            postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
            postReq.ContentLength = messagebyte.Length

            '//Send Request
            System.Net.ServicePointManager.Expect100Continue = False

            Dim postreqstream As Stream = postReq.GetRequestStream()
            postreqstream.Write(messagebyte, 0, messagebyte.Length)
            postreqstream.Close()
            Dim postresponse As HttpWebResponse

            postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
            tempcookies.Add(postresponse.Cookies)
            logincookie = tempcookies

            Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

            'The Response Text
            Dim thepage As String = postreqreader.ReadToEnd
            thepage = thepage.Replace(Chr(34), Chr(39))

            Return thepage

        Catch e As Exception
            Return False

        End Try

    End Function