Necessary for the development of medium and large projects in. NET -- http request call (Post and Get)

Posted by mastermike707 on Wed, 26 Jan 2022 01:21:38 +0100

Related downloads:

DeveloperSharp.dll component
DeveloperSharp.dll component

 

http request invocation is a function often used in development. It will be used when calling the Web Api and other formal interfaces of its own project; In addition, it will also be used when calling some third-party function interfaces, because these third-party functions are often provided in the form of http address, such as SMS service, online translation, map service, voice intelligence, etc

 

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

The request in the form of Post is relatively complex and more commonly used. Once you understand it, you can understand it in the form of Get. So let's first look at the request call in the form of Post.

When using Post to call http, four parameters need to be set:

(1) Call address

(2) Need to send past parameters

(3) http request header (if set)

(4) Encoding format. Commonly used: application/json

 

The following is a code example of "sending SMS" using a third-party link:

using DeveloperSharp.Framework.CoreUtility; //quote DeveloperSharp.dll
using Newtonsoft.Json; //quote Newtonsoft.Json.dll
--------------------------

        public static object iSoftStoneSync(string mobile, string code, string name)
        {
            //establish DeveloperSharp Medium IUtility tool
            IUtility IU = new Utility();

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

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

In the above example, because "http request header" is not set, the value of the third parameter of HttpPost method is null.

 

The HttpPost method is described in detail as follows:

HttpPost
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 requested url address
     (2)string ParamData      --  Submitted parameters
     (3)Dictionary<string, string> HeaderDic  --  deposit http Key value pair of header
     (4)string ContentType   --  The encoding format of the request, usually application/x-www-form-urlencoded(Default setting) multipart/form-data,application/json Three forms
 return: String   --  Request result(-107 (the beginning indicates an error)

 

 

Finally, let's talk about the request call in the form of Get.

The Get form is often used for calls with "? Name1 = value1 & Name2 = Value2 & name3 = value3" after the url. The characteristic of this kind of call is that the "parameters to be sent" are directly hung after the "call address". The following is a direct example, which you can see at a glance:

using DeveloperSharp.Framework.CoreUtility; //quote DeveloperSharp.dll
--------------------------

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

 

[note]: the download examples given at the beginning of the article have been successfully run. However, download examples often contain only "core template" content. Some auxiliary contents need to be created / set by yourself (such as database creation, link string setting, file configuration, path setting, parameter setting, etc.).
Please do an example experiment on the basis of understanding the content of the article. In case of abnormal error, please read + understand this article carefully again.
If you do encounter difficulties, please scan the QR code at the end of the text on wechat and contact technical support!

Topics: C# .NET post get