Danke für die Antwort.
Daten auslesen ist über die WebAPI direkt kein Problem, Authorisierung usw klappt.
Beim Versuch einen Kunden anzulegen bekomme ich immer ein 500 zurück.
Kann mir jemand sagen was ich hier falsch mache????
C# Console .....
using System.Security.Cryptography;
static void Main(string[] args)
{
InsertCustomer();
}
private static void InsertCustomer()
{
string content = "{\"CustomerGuid\":\"65d1fca9-a7c7-4bbe-9e91-38165d20d491\"," +
"\"Username\":\"DevAdmin2\"," +
"\"Email\":\"b.bartha2@itatbusiness.de\"," +
"\"IsTaxExempt\":false," +
"\"AffiliateId\":0," +
"\"Active\":true," +
"\"IsSystemAccount\":false," +
"\"Id\":39}";
byte[] data = Encoding.UTF8.GetBytes(content);
string contHash = CreateContentMd5Hash(data);
string publicKey = "687d2b629ce9eef4f05c1415437c1fa0";
string secretKey = "47bd9250f6f2914815ddf056e6ae884f";
string method = "post";
string accept = "application/json, text/javascript, */*";
string timestamp = DateTime.UtcNow.ToString("o"); // 2013-11-11T10:15:54.1731069Z
string messageRepresentation = string.Join("\n",
method.ToLower(),
contHash,
accept.ToLower(),
url.ToLower(),
timestamp,
publicKey.ToLower()
);
Console.WriteLine(messageRepresentation);
string signature = CreateSignature(secretKey, messageRepresentation); // hWce6V2KA0kkB0GBbIK0GSw5QAcS3+vj+m+WN/8k9EE=
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;
request.UserAgent = "My shopping data consumer v.1.0";
request.Accept = accept;
request.Headers.Add("Accept-Charset", "UTF-8");
request.Headers.Add("SmartStore-Net-Api-PublicKey", publicKey);
request.Headers.Add("SmartStore-Net-Api-Date", timestamp);
request.Headers.Add("Authorization", "SmNetHmac1 " + signature);
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = data.Length;
request.Headers.Add("Content-MD5", contHash); //optional
for (int i = 0; i < request.Headers.Count; i++)
{
Console.WriteLine("H {0} {1}", request.Headers.GetKey(i), request.Headers.GetValues(i)[0]);
}
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = request.GetResponse(); //<- Exception: 500
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
var dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
Console.WriteLine(reader.ReadToEnd());
}
public static string CreateContentMd5Hash(byte[] content)
{
string result = "";
if (content != null && content.Length > 0)
{
using (var md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(content);
result = Convert.ToBase64String(hash);
}
}
return result;
}
public static string CreateSignature(string secretKey, string messageRepresentation)
{
if (string.IsNullOrWhiteSpace(secretKey) || string.IsNullOrWhiteSpace(messageRepresentation))
return "";
string signature;
var secretBytes = Encoding.UTF8.GetBytes(secretKey);
var valueBytes = Encoding.UTF8.GetBytes(messageRepresentation);
using (var hmac = new HMACSHA256(secretBytes))
{
var hash = hmac.ComputeHash(valueBytes);
signature = Convert.ToBase64String(hash);
}
return signature;
}