SoFunction
Updated on 2025-03-07

c# Send an instance requesting access to an external interface

I won't say much nonsense, let's just read the code~

 string url = "/smsapi/sms/verifycode";
   HttpClient httpClient = new HttpClient();
    = new Uri(url);
   //Table header parameters   string token = "9c0025b4aae442bda5498971ec1ab219";
   ("token", token);
   ();
   (new MediaTypeWithQualityHeaderValue("application/json"));
   try
   {
    using (var request = new HttpRequestMessage())
    {
     var postBody = $"{{\"identity\":\"{identity}\",\"phoneNumber\":\"{phoneNumber}\",\"code\":\"[code]\"}}";
      = ;
      = new StringContent(postBody, Encoding.UTF8, "application/json");
     var response = await (request);
     //Return result     var retString = await ();
     JObject items = (JObject)(retString);
     //Return code, successful 200     string retCode = items["code"].ToString();
     Return message
     string message = items["message"].ToString();
     string data = items["data"].ToString();
     JObject items_data = (JObject)(data);
     string isSuccess = items_data["isSuccess"].ToString();
     string data_message = items_data["message"].ToString();
     if (isSuccess == "False")
     {
      retCode = "300";
     }
      = retCode;
      = data_message;
    }
   }
   catch (Exception e)
   {
     = "300";
     = ();
   }
 
   return retMessage;
  }
 public static async Task<string> PostData(string path,string serverPath,dynamic param) {
   try
   {
    HttpClient client = new HttpClient();
   
    ("Referer", ("http://{0}", serverPath));
    HttpResponseMessage response;
    using (HttpContent httpContent = new StringContent(param, Encoding.UTF8))
    { 
     
      = new ("application/json");
     //("x-token", "M3Q_mQEwmn9l4Ly7fM7DxaMSdlYSSup5lfKLztF_VP97Urf");
     response = await (path, httpContent).ContinueWith(res =>
     {
 
      return res;
 
     }).Result;
    }
    if (response != null && )
    {
     return await ();
 
    }
    else
    {
     return ;
    }
   }
   catch (Exception ex)
   {
 
    throw;
   }
   finally
   {
   }
  }

Supplement: C# background calls Http external network interface (GET, POST)

Method calling interface to get json file content

public void GetFunction()
  {   
   string serviceAddress = "http://222.111.999.444:8687/tttr/usercrd/12/b7e50cb45a?userid=9999";
   HttpWebRequest request = (HttpWebRequest)(serviceAddress);
    = "GET";
    = "text/html;charset=UTF-8";
   HttpWebResponse response = (HttpWebResponse)();
   Stream myResponseStream = ();
   StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
   string retString = ();
   ();
   ();
   (retString);
  }

This is too complicated. I suddenly found a simple one:

using (var client = new WebClient()){
  = Encoding.UTF8;
 string serviceAddress = urlappend + "cloud/device/data/getErrorData?appId=" + appid + "&amp;accessToken=" + accessToken + "&amp;timestamp=" + time + "&amp;deviceIds=" + deviceIds;
  var data = (serviceAddress);
  var obj = &lt;JObject&gt;(data);
}
//obj is the object that returns data

Method calling interface to get json file content

public void PostFunction()
  {
   string serviceAddress = "http://222.111.999.444:8687/tttr/usercrd/uuu/12/dfd7e4";
   HttpWebRequest request = (HttpWebRequest)(serviceAddress);
    = "POST";
    = "application/json";
   string strContent = @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}";
   using (StreamWriter dataStream = new StreamWriter(()))
   {
    (strContent);
    ();
   }
   HttpWebResponse response = (HttpWebResponse)();
   string encoding = ;
   if (encoding == null ||  &lt; 1)
   {
    encoding = "UTF-8"; //Default encoding   }
   StreamReader reader = new StreamReader((), (encoding));
   string retString = ();
   //Parse json   JObject jo = (retString);
   (jo["message"]["mmmm"].ToString());   
  } 

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.