使用HttpClient从c#调用Shopware API

时间:2018-06-08 16:00:46

标签: c# curl httpclient shopware

我正在尝试从c#调用Shopware REST API。 Shopware有使用curl调用API的文档,主要是我可以将它转换为c#和HttpClient,但对于某些选项我只是不知道要设置的标题:

The Shop是一个基本的htaccess-auth,并使用apikey进行Shopware认证。到目前为止我的代码:

var handler = new System.Net.Http.HttpClientHandler { Credentials = new NetworkCredential(htaccessUsername, htaccessPassword) });
var client = new System.Net.Http.HttpClient(handler);

using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, apiUrl + "orders?limit=20"))
{
  var encodedStr = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{apiKey}"));
  var authorizationKey = "Basic" + " " + encodedStr;
  requestMessage.Headers.Add("Authorization", authorizationKey);
  // curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
  // curl_setopt($this->cURL, CURLOPT_FOLLOWLOCATION, false);
  // curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  // curl_setopt(
  //    $this->cURL,
  //    CURLOPT_HTTPHEADER,
  //    ['Content-Type: application/json; charset=utf-8']
  // );
  using (var responseMessage = await client.SendAsync(requestMessage))
  {
    var data = await responseMessage.Content.ReadAsStringAsync();
    System.Diagnostics.Trace.WriteLine(data);
  }
}

基本的htaccess auth正在运行,但Shopware身份验证失败,数据中出现以下响应:

"{\"success\":false,\"message\":\"Invalid or missing auth\"}"

我想我需要以某种方式在c#中实现curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);,但我没有发现如何将这些curl选项转换为标题。 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

看起来你的答案是here

0.6