Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to practice the Interface of WebApiClient Baidu Map Service

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to carry out WebApiClient Baidu map service interface practice, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Purpose of the article

With the continuous improvement of WebApiClient, more and more developers choose WebApiClient to replace the native HttpClient. However, when applied to actual projects, they will more or less encounter some questions and difficulties in the combination of projects. As WebApiClient users, we will use WebApiClient in the Asp.net core mvc project to request Baidu map service interface to demonstrate the skills of using WebApiClient.

two。 Baidu Map Service Interface

Static graph interface

Http://api.map.baidu.com/staticimage/v2?ak= your key & mcode=666666¢er=116.403874,39.914888&width=300&height=200&zoom=11

Coordinate conversion interface

Http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924&from=1&to=5&ak=, your key.

3. Interface analysis

By analyzing the interface of Baidu map, we find that:

All interfaces are on the domain name api.map.baidu.com.

The ak parameter is a parameter for client identification, which needs to be appended to all request interfaces.

The parameter value of 116.403874 and 39.914888 required in the interface is actually (longitude, latitude), which is composed of two values.

From and to are enumerated numeric types

When we do Coding, we should focus on these commonalities in order to reduce repetitive work.

4. Interface statement 4.1Public domain name [HttpHost ("http://api.map.baidu.com/")]

Public interface IBdMapApi: IHttpApi {} 4.2 common AK parameter

We want to implement an interface-level or method-level ApiAction feature to add common ak parameters to the request path:

Then append the Ak feature to the interface:

[HttpHost ("http://api.map.baidu.com/")][Ak("qde9uxuEhwMlngvTbWGo3BIQOKfqvjdc")]

Public interface IBdMapApi: IHttpApi {} 4.3 static diagram staticimage interface

Refer to the interface documentation and declare the original StaticImage interface:

At present, the center parameter is of string type, which is very weak in constraint, which is quite different from the format data required by the API (longitude, latitude). So we should define a BdLocation type that inherits from IApiParameterable and convert the incoming longitude and latitude to this text format:

After modification, the StaticImage API is modified to:

[HttpGet ("staticimage/v2")]

ITask StaticImageAsync (BdLocation center, int width = 300, int height = 200, int zoom = 11, int mcode = 666666)

Write the original interface according to the documentation

[HttpGet ("geoconv/v1/")]

ITask GeoconvAsync (string coords, int from = 1, int to = 5)

Like StaticImage, we also need to modify the parameter constraints of this API. Coords is actually a BdLocation type, from and to can be modified to enumerated types, and the return value string is changed to a strongly typed model. The modified API is:

[HttpGet ("geoconv/v1/")] ITask GeoconvAsync (BdLocation coords, BdFrom from = BdFrom.wgs84, BdTo to = BdTo.bd09ll); 4.5 complete interface declaration

5. Dependency injection of interface

The proxy instance created by HttpApiClient of WebApiClient is suitable for singleton mode. In the development of projects that support dependency injection, dependency injection should be used as far as possible to complete HttpApiClient creation and lifecycle management.

Dependency injection of 5.1 Asp.net core

Add the injection configuration of IBdMapApi to the ConfigureServices method

If many interfaces are declared in the project, such as IBaiduApi, ITengxunApi, etc., you can circulate batch injection:

5.2 receiving and using IBdMapApi proxy instances

6. Monitor the content of the request submission

WebApiClient abstracts Http requests with a high degree of abstraction, which is only declared but not implemented. If we are not familiar with WebApiClient, we may need to monitor the content of the request to see if it meets the interface data requirements of the server. Without using third-party tools such as Fiddler, we can modify a custom filter for the interface to achieve the ability to access, print and seek message content in the filter.

6.1 define TraceFilter filter

6.2 Interface Association TraceFilter

6.3 View request content

Turn on the program debugging and print in the output window

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report