Home >Backend Development >C#.Net Tutorial >C# solves the problem of Baidu Map API APP SN verification failure (code example)
This article will introduce to you the solution to the SN verification failure problem encountered when using C# to call Baidu Map API. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The signature (sn verification) problem encountered when using C# to call Baidu Map Web Service API is recorded here. ( Please ignore the IP whitelist verification )
1. First get ak and sk, these two things can be obtained from the console
2. At this address: sn signature algorithm, which provides java, php, c#, python2.7 Reference code
In the reference code provided by Baidu, there is a problem with the MD5 encryption method in the C# code. (When the author wrote the essay, there was still a problem with the code. I hope Baidu will officially fix this problem later)
Modify Baidu's MD5 method, the signature will be correct, and the API will be called normally, happy! !
Here is the complete signature code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace IpService.Code { public class BaiduAKSNCaculater { private static string MD5(string password) { try { System.Security.Cryptography.HashAlgorithm hash = System.Security.Cryptography.MD5.Create(); byte[] hash_out = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); var md5_str=BitConverter.ToString(hash_out).Replace("-", ""); return md5_str.ToLower(); } catch { throw; } } private static string UrlEncode(string str) { str = System.Web.HttpUtility.UrlEncode(str); byte[] buf = Encoding.ASCII.GetBytes(str);//等同于Encoding.ASCII.GetBytes(str) for (int i = 0; i < buf.Length; i++) if (buf[i] == '%') { if (buf[i + 1] >= 'a') buf[i + 1] -= 32; if (buf[i + 2] >= 'a') buf[i + 2] -= 32; i += 2; } return Encoding.ASCII.GetString(buf);//同上,等同于Encoding.ASCII.GetString(buf) } private static string HttpBuildQuery(IDictionary<string, string> querystring_arrays) { StringBuilder sb = new StringBuilder(); foreach (var item in querystring_arrays) { sb.Append(UrlEncode(item.Key)); sb.Append("="); sb.Append(UrlEncode(item.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); return sb.ToString(); } public static string CaculateAKSN(string ak, string sk, string url, IDictionary<string, string> querystring_arrays) { var queryString = HttpBuildQuery(querystring_arrays); var str = UrlEncode(url + "?" + queryString + sk); return MD5(str); } } }
Here is the calling code
public static string GetIPAreas() { var ip = "你要查询的ip地址"; var ak = "从百度控制台获取到应用AK"; var sk = "从百度控制台获取到签名SK"; var uri = "http://api.map.baidu.com"; var path = "/location/ip"; var param = new Dictionary<string, string>(); param.Add("ip", ip); param.Add("ak", ak); //注意:签名的url参数,并非完整地址 var sn = BaiduAKSNCaculater.CaculateAKSN(ak, sk, path, param); var url = string.Format("{0}{1}?ip={2}&ak={3}&sn={4}", uri, path, ip, ak, sn); var str = Code.HttpService.Get(url); return str; }
Written at the end, I hope Baidu officials will modify the code in the example as soon as possible so that others can use it Use
to summarize: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit C#Video Tutorial!
The above is the detailed content of C# solves the problem of Baidu Map API APP SN verification failure (code example). For more information, please follow other related articles on the PHP Chinese website!