I had some trouble with the API request against the bf3stats.com/api and I thought I just should spare you that struggle with showing you my test console app. The problem was making a Signature (sig) without using the UTF8 encoding, just do it directly on the byte[] array.
Just switch the API keys to your own, and a player you want to update...
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Web; using System.Collections; using System.Web.Security; using System.Security.Cryptography; namespace BF3 { class Program { static string BF3APIIdent = "xxxx"; static string BF3APIKey = "xxxx"; static void Main(string[] args) { Console.WriteLine(updatePlayer("pc", "PlayersName")); Console.ReadLine(); } public static string MakeUrlSafe(string code) { if (string.IsNullOrEmpty(code)) return string.Empty; return code.Replace("+", "-").Replace("/", "_").Replace("=", string.Empty); } public static string updatePlayer(string platform, string name) { Hashtable data = new Hashtable(); data["ident"] = BF3APIIdent; data["time"] = time(); data["player"] = name; return getAPIDataSigned("playerupdate", platform, data); } public static string getAPIDataSigned(string type, string platform, Hashtable data) { System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(data); string bdata = MakeUrlSafe(Base64Encode(sJSON)); string sig = Sig(bdata, BF3APIKey); string postdata = "data=" + bdata + "&sig=" + sig; return BF3Request(postdata, "http://api.bf3stats.com/" + platform + "/" + type + "/"); } static string BF3Request(string data, string url) { System.Net.ServicePointManager.Expect100Continue = false; //IMPORTANT otherwise 417 Exception WebRequest request = WebRequest.Create(url); request.Method = "POST"; string postData = data; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string item = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return item; } public static string Sig(string data, string key) { var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(key)); var bytes = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(data)); return MakeUrlSafe(Base64Encode(bytes)); } public static string Base64Encode(string data) { byte[] encData_byte = new byte[data.Length]; encData_byte = System.Text.Encoding.UTF8.GetBytes(data); string encodedData = Convert.ToBase64String(encData_byte); return encodedData; } public static string Base64Encode(byte[] encData_byte) { return Convert.ToBase64String(encData_byte); } private static long time() { return time(DateTime.UtcNow); } private static long time(DateTime time) { DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0); TimeSpan span = time.Subtract(unixEpoch); return (long)span.TotalSeconds; } } }
Inga kommentarer:
Skicka en kommentar