在RestSharp中复制System.Net.Http方法

时间:2017-12-16 02:47:02

标签: c# restsharp

我正在尝试使用RestSharp复制一个调用Web API的示例,但我遇到了一些问题。

以下是示例

df.groupby('TAG').WORD.apply(list).apply(pd.Series).T
Out[221]: 
TAG  ADP   DET      NOUN PART PROPN   VERB
0     in  This    sample   to   NLP     is
1    NaN     a  sentence  NaN   NaN  check
2    NaN   NaN    python  NaN   NaN    NaN

我认为我遇到的问题是签名变量未正确创建。我正在使用

设置RestSharp请求的主体
df.groupby('TAG').WORD.apply(list).apply(pd.Series).T.fillna('')
Out[222]: 
TAG ADP   DET      NOUN PART PROPN   VERB
0    in  This    sample   to   NLP     is
1           a  sentence             check
2                python                  

要获取正文request.content我正在使用

string apiKey = "my key";
string apiSecret = "my secret";
string requestUri = "https://www.cryptopia.co.nz/Api/GetBalance";
var postData = new
{
    Currency = "DOT"
};

// Create Request
var request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(requestUri);
request.Content = new ObjectContent(typeof(object), postData, new JsonMediaTypeFormatter());

// Authentication
string requestContentBase64String = string.Empty;
if (request.Content != null)
{
   // Hash content to ensure message integrity
   using (var md5 = MD5.Create())
   {
      requestContentBase64String = Convert.ToBase64String(md5.ComputeHash(await request.Content.ReadAsByteArrayAsync()));
   }
}

//create random nonce for each request
var nonce = Guid.NewGuid().ToString("N");

//Creating the raw signature string
var signature = Encoding.UTF8.GetBytes(string.Concat(apiKey, HttpMethod.Post, HttpUtility.UrlEncode(request.RequestUri.AbsoluteUri.ToLower()), nonce, requestContentBase64String));
using (var hmac = new HMACSHA256(Convert.FromBase64String(apiSecret)))
{
   request.Headers.Authorization = new AuthenticationHeaderValue("amx", string.Format("{0}:{1}:{2}", apiKey, Convert.ToBase64String(hmac.ComputeHash(signature)), nonce));
}


// Send Request
using (var client = new HttpClient())
{
   var response = await client.SendAsync(request);
   if (response.IsSuccessStatusCode)
   {
      Console.WriteLine(await response.Content.ReadAsStringAsync());
      //{"Success":true,"Error":null,"Data":[{"CurrencyId":2,"Symbol":"DOT","Total":9646.07411016,"Available":9646.07411016,"Unconfirmed":0.0,"HeldForTrades":0.0,"PendingWithdraw":0.0,"Address":"1HEfio1kreDBgj5uCw4VHbEDSgc6YJXfTN","Status":"OK","StatusMessage":null}]}
   }
}

获取requestContentBase64String我正在使用

request.AddJsonBody(request.JsonSerializer.Serialize(new { Currency = "DOT" }));

最后添加我正在使用的授权标题

var body = request.Parameters.Where(p => p.Type == ParameterType.RequestBody).FirstOrDefault().Value.ToString();

服务器的响应是

var requestSignatureBase64String = Convert.ToBase64String(signatureBytes);

0 个答案:

没有答案