1
    <?php

  function bter_query($path, array $req = array()) {
        // API settings, add your Key and Secret at here
        $key = 'key here';
        $secret = 'secrethere';

        // generate a nonce to avoid problems with 32bits systems
        $mt = explode(' ', microtime());
        $req['nonce'] = $mt[1].substr($mt[0], 2, 6);

        // generate the POST data string
        $post_data = http_build_query($req, '', '&');
        $sign = hash_hmac('sha512', $post_data, $secret);

        // generate the extra headers
        $headers = array(
            'KEY: '.$key,
            'SIGN: '.$sign,
        );


        // curl handle (initialize if required)
    static $ch = null;
        if (is_null($ch)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 
                'Mozilla/4.0 (compatible; Bter PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
                );
        }
        curl_setopt($ch, CURLOPT_URL, 'https://bter.com/api/'.$path);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        // run the query
        $res = curl_exec($ch);

        if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
        //echo $res;
        $dec = json_decode($res, true);
        if (!$dec) throw new Exception('Invalid data: '.$res);
        return $dec;
    }


    try {
        // example 1: get funds
        var_dump(bter_query('1/private/getfunds'));

    } catch (Exception $e) {
        echo "Error:".$e->getMessage();

    } 
?> 

大家好,我需要将上面的 PHP 代码转换为 VBNET 我正在使用以下函数:

Private Function StringToSHA512(ByVal content As String) As String
    Dim M5 As New SHA512Managed
    Dim bytestring() As Byte = Encoding.UTF8.GetBytes(content)
    bytestring = M5.ComputeHash(bytestring)
    Dim signer As String = Nothing
    For Each bt As Byte In bytestring
        signer &= bt.ToString("x2")
    Next
    Return signer
End Function

Private Function GetUnixTimestamp(ByVal currDate As DateTime) As Integer
    'create Timespan by subtracting the value provided from the Unix Epoch
    Dim span As TimeSpan = (currDate - New DateTime(2012, 1, 1, 0, 0, 0, 0).ToLocalTime())
    'return the total seconds (which is a UNIX timestamp)
    Return span.TotalSeconds
End Function

    Private Function response(ByVal postdata As String) As String
        Dim postReq As HttpWebRequest
        Try
            postReq = DirectCast(WebRequest.Create("https://bter.com/api/1/private/getfunds"), HttpWebRequest)
            Dim keyer As String = LCase("Keyhere") 'your key goes here
            Dim secret As String = "SecretHere" 'your secret goes here
            Dim KeyByte() As Byte = Encoding.ASCII.GetBytes(secret)
            Dim HMAcSha As New HMACSHA512(Encoding.ASCII.GetBytes(secret))
            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("-", "")
            postReq.Method = "POST"
            postReq.KeepAlive = False
            postReq.Headers.Add("Key", keyer)
            postReq.Headers.Add("Sign", LCase(Sign))
            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
            Dim postreqstream As Stream = postReq.GetRequestStream()
            postreqstream.Write(messagebyte, 0, messagebyte.Length)
            postreqstream.Dispose()
            Dim PostRes As HttpWebResponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
            Dim postreqreader As New StreamReader(PostRes.GetResponseStream())
            response = postreqreader.ReadToEnd.Replace(Chr(34), Chr(39))
            postreqreader.Dispose()
            PostRes.Close()
        Catch ex As Exception
            response = ""
        End Try
    End Function

但是当我调用 response("nonce=" & GetUnixtimestamp(Now)) 它返回:

{'result':'false','message':'错误:无效数据'}

我究竟做错了什么?

4

1 回答 1

0

知道这是一个旧线程,但我/正在遇到同样的问题。这是我的解决方案:

randomn = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalSeconds)
postData = "method=" & lstmeth.SelectedItem & "&nonce=" & randomn
于 2013-11-02T07:57:31.313 回答