


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
