1024programmer Asp.Net Let’s talk about http request calls (Post and Get), the core part of project research and development

Let’s talk about http request calls (Post and Get), the core part of project research and development

Let’s talk about http request calls (Post and Get), the core part of project research and development

Supports .Net Core (2.0 and above) and .Net Framework (4.0 and above)

【Directory】

  1. Foreword

  2. Post request

  3. Get request

  4. Comparison with other tools

1【Foreword】

http request calling is a function often used in development.

Internally, it will be used when calling the Web Api and other formal interfaces of its own projects; externally, it will also be used when calling some third-party functional interfaces, because these third-party functions are often provided in the form of http addresses. Such as: SMS service, online translation, map service, voice intelligence, etc…

So, http request calling is often a necessary core part of the development of medium and large projects, and every engineer must master it! !

2【Post request】

http request calls are divided into two forms: Post and Get.

The Post form of request is relatively complex and more commonly used. Once you understand it, the Get form will be easy to understand at a glance, so let’s first take a look at the Post form of request call.

When using Post to call http, there are often four parameters that need to be set:

(1) Calling address

(2) Need to send past parameters

(3) http request header (if set)

(4) Coding format. Commonly used ones are: application/json

The following is a code example of using a third-party link to “send SMS”:

using DeveloperSharp.Framework.CoreUtility; //Reference the DeveloperSharp package from NuGet
 using Newtonsoft.Json; //Reference the Newtonsoft.Json package from NuGet
 --------------------------

         public static object SendMessage(string mobile,  string code, string   name)
         {
             //Create DeveloperSharp  IUtility tools
             IUtility IU = new Utility();

             //Calling address  
             string requestUrl = "  https://prekaka.rainfn.com/kaka/api/v1/activity/uploadUserData";
             //Need to send  Parameters
             Dictionary<string, object> data = new Dictionary<string  , object>
             {
                 { "mobile",mobile },
                 { "code",code},
                 { "name",name}
             };

             //Post format  Send request call
             var responsesStr = IU.HttpPost(requestUrl, JsonConvert.SerializeObject(data), null, "application/json");
             object reData = JsonConvert.DeserializeObject<object>(responsesStr);
             return reData;
         }

Because the “http request header” is not set in the above example, the third parameter of the HttpPost method is null.

The detailed description of the HttpPost method is as follows:

HttpPost (asynchronous method: HttpPostAsync)
 Statement: string HttpPost(string Url, string ParamData = "", Dictionary<string, string> HeaderDic = null, string ContentType = "application/x-www-form-urlencoded");
 Purpose: Call Http-Post request
 Parameters: (1)string Url -- Call the url address of the request
      (2) string   ParamData -- Submitted parameters
      (3) Dictionary<string, string> HeaderDic -- Store key-value pairs of http headers
      (4)string   ContentType -- The requested encoding format, usually application/x-www-form-urlencoded (default setting), multipart/form-data, application/json three forms
 Return: String --Request result (starting with -107 indicates an error) 

3【Get request】

Then, let’s talk about the request call in the form of Get.

The Get mode is often used for calls with a question mark after the URL “?N1=V1&N2=V2&N3=V3”. The characteristic of this type of call is that the “parameters that need to be sent” are directly linked to the “calling address”. Here is an example directly, you will know at a glance:

using DeveloperSharp.Framework.CoreUtility; //Reference the DeveloperSharp package from NuGet
 --------------------------

       IUtility ui = new  Utility();
       string r = ui.HttpGet("http://localhost:1416/Handler1.ashx?name=kyyk&age=100");

4【Comparison with other tools】

Disclaimer: This part will trigger a war of words. Try to reduce the sensitivity. Experts please do not spray. If you don’t You don’t have to use this article’s solution if you like, no one is forcing you to take sides!

(1) Some people may say that http calls also have Put, Delete, and other types. But in actual development, we only use Post/Get.

(2) It is precisely because http request calling is such an important part of project development that many related tools have been produced on the market, such as: FlurXXX, RestSharXXX, HttpClieXXX, etc…

But the solution in this article can actually be regarded as the above-mentioned solutionTheoretical basis/bottom layer .

Some of the above tools simplify the packaging and integration of the two steps of “encoding format” and “json serialization/deserialization” in this article’s solution, and have become PostJson, GetUrl, and other methods. But as mentioned earlier in this article, the commonly used “encoding format” Content-Types are Json, x-www-form-urlencoded, and form-data. They are just the three commonly used ones. In fact, there are more than these three (this is the problem) The core of it!!).

You can now package it into PostJson, PostUrl, and PostData. Will authors package more types in the future? The answer is: no! !
So, in this sense, the above tools are functionally defective and cannot handle special situations! !

In order to make the function complete and be able to handle http request calls in all situations, the best way is to expose the “encoding format” Content-Type as a string parameter like the solution in this article, allowing developers to make their own decisions based on the situation. Fill in….(End of article)

Original link: http://www.developersharp.cc/content9.html

Ending


The following is my official account, which contains many high-value technical articles, which are experiences that you cannot accumulate even if you work hard, and can help individuals grow rapidly.

You can also join us (add WeChat: 894988403, remark “join the group”), learn from the big guys, explore the industry insiders, and enjoy the opportunities of the times.

�: No! !
So, in this sense, the above tools are functionally defective and cannot handle special situations! !

In order to make the function complete and be able to handle http request calls in all situations, the best way is to expose the “encoding format” Content-Type as a string parameter like the solution in this article, allowing developers to make their own decisions based on the situation. Fill in….(End of article)

Original link: http://www.developersharp.cc/content9.html

Ending


The following is my official account, which contains many high-value technical articles, which are experiences that you cannot accumulate even if you work hard, and can help individuals grow rapidly.

You can also join us (add WeChat: 894988403, remark “join the group”), learn from the big guys, explore the industry insiders, and enjoy the opportunities of the times.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/lets-talk-about-http-request-calls-post-and-get-the-core-part-of-project-research-and-development/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索