Home Backend Development C#.Net Tutorial C# solves the problem of Baidu Map API APP SN verification failure (code example)

C# solves the problem of Baidu Map API APP SN verification failure (code example)

Oct 18, 2018 pm 02:32 PM
c# sign call api

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] == &#39;%&#39;)
                {
                    if (buf[i + 1] >= &#39;a&#39;) buf[i + 1] -= 32;
                    if (buf[i + 2] >= &#39;a&#39;) 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);
        }
    }
}
Copy after login

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;
        }
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

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

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

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

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

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

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

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.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

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.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

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

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

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 c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

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.

See all articles