使用 HttpClient 从 C# 调用 Shopware API
Call Shopware API from c# with HttpClient
我正在尝试从 c# 调用 Shopware REST API。 Shopware 有使用 curl 调用 API 的文档,大多数情况下我可以将其转换为 c# 和 HttpClient,但对于某些选项我只是不知道要设置什么 header:
商店在基本的 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 身份验证有效,但 Shopware 身份验证确实失败,数据中出现以下响应:
"{\"success\":false,\"message\":\"Invalid or missing auth\"}"
我想我需要以某种方式在 C# 中实现 curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
,但我不知道如何将这些卷曲选项转换为 header。
有帮助吗?
看起来你的答案是 here:
var credCache = new CredentialCache();
var basicCred = new NetworkCredential(htaccessUsername, htaccessPassword);
var digestCred = new NetworkCredential(username, apiKey);
credCache.Add(new Uri("http://.com/"), "Basic", basicCred);
credCache.Add(new Uri("http://.com/"), "Digest", digestCred);
var httpClient = new HttpClient(new HttpClientHandler { Credentials = credCache });
我正在尝试从 c# 调用 Shopware REST API。 Shopware 有使用 curl 调用 API 的文档,大多数情况下我可以将其转换为 c# 和 HttpClient,但对于某些选项我只是不知道要设置什么 header:
商店在基本的 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 身份验证有效,但 Shopware 身份验证确实失败,数据中出现以下响应:
"{\"success\":false,\"message\":\"Invalid or missing auth\"}"
我想我需要以某种方式在 C# 中实现 curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
,但我不知道如何将这些卷曲选项转换为 header。
有帮助吗?
看起来你的答案是 here:
var credCache = new CredentialCache();
var basicCred = new NetworkCredential(htaccessUsername, htaccessPassword);
var digestCred = new NetworkCredential(username, apiKey);
credCache.Add(new Uri("http://.com/"), "Basic", basicCred);
credCache.Add(new Uri("http://.com/"), "Digest", digestCred);
var httpClient = new HttpClient(new HttpClientHandler { Credentials = credCache });