dongwei1895 2017-11-17 02:16
浏览 65

php hash_hmac到c# - webresponse“NONCE_NOT_PROVIDED”

I try to convert a php-code to c#

php

$apikey='xxx';
$apisecretkey='yyy';
$nonce=time();
$uri='https://website.com/api/getsomething?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecretkey);
$resource = curl_init($uri);
curl_setopt($resource, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($resource);
$obj = ($execResult);

and here my C#

WebRequest _WebRequest;
WebResponse _WebResponse;
string apikey = "xxx";
string apisecret = "yyy";
string nonce = ((int)(DateTime.UtcNow - new DateTime(1970,1,1)).TotalSeconds).ToString();
string uri = $"https://website.com/api/getsomething?apikey={apikey}nonce={nonce}";
string _Hash_hmac = Hash_hmac(uri, apisecret);

_WebRequest = WebRequest.Create(uri);
_WebRequest.Headers["apisign"] = _Hash_hmac;

_WebResponse = _WebRequest.GetResponse();

The Code for Hash-hmac I use this C# equivalent to hash_hmac in PHP

The Webresponse is always "NONCE_NOT_PROVIDED"

I checked the outcome from the hash_hmac with the one in php - they are the same. And also try string ToUpper and ToLower.

What I am doing wrong?

  • 写回答

1条回答

  • dsvjmc0907 2017-11-17 02:18
    关注

    Aren't you missing an ampersand (&)?

    apikey={apikey}nonce={nonce}
    

    becomes

    apikey={apikey}&nonce={nonce}
    

    I would advise reading your code more closely with these kinds of errors.

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python