SoFunction
Updated on 2025-04-07

Implementing mobile phone number home interface call based on C#

This article introduces the implementation of the mobile phone number home address interface call based on C#. It is shared with you for your reference. The specific content is as follows

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
//----------------------------------
// Sample code for calling mobile number home location - Aggregate data// Online interface document: /docs/11// Download address of JsonObject class in the code: /download/gcm3206021155665/7458439//----------------------------------
 
namespace ConsoleAPI
{
  class Program
  {
    static void Main(string[] args)
    {
      string appkey = "*******************"; //Configure the appkey you applied for 
       
      //1. Mobile phone home location query      string url1 = "/mobile/get";
 
      var parameters1 = new Dictionary<string, string>();
 
      ("phone" , ""); //The first 7 digits of mobile phone number or mobile phone number that need to be inquired      ("key", appkey);//The key you applied for      ("dtype" , ""); //Return the data format, xml or json, default json 
      string result1 = sendPost(url1, parameters1, "get");
 
      JsonObject newObj1 = new JsonObject(result1);
      String errorCode1 = newObj1["error_code"].Value;
 
      if (errorCode1 == "0")
      {
        ("success");
        (newObj1);
      }
      else
      {
        //("fail");        (newObj1["error_code"].Value+":"+newObj1["reason"].Value);
      }
 
 
    }
 
    /// <summary>
    /// Http (GET/POST)
    /// </summary>
    /// <param name="url">Request URL</param>    /// <param name="parameters">Request parameters</param>    /// <param name="method">request method</param>    /// <returns>Response content</returns>    static string sendPost(string url, IDictionary&lt;string, string&gt; parameters, string method)
    {
      if (() == "post")
      {
        HttpWebRequest req = null;
        HttpWebResponse rsp = null;
         reqStream = null;
        try
        {
          req = (HttpWebRequest)(url);
           = method;
           = false;
           = HttpVersion.Version10;
           = 5000;
           = "application/x-www-form-urlencoded;charset=utf-8";
          byte[] postData = Encoding.(BuildQuery(parameters, "utf8"));
          reqStream = ();
          (postData, 0, );
          rsp = (HttpWebResponse)();
          Encoding encoding = ();
          return GetResponseAsString(rsp, encoding);
        }
        catch (Exception ex)
        {
          return ;
        }
        finally
        {
          if (reqStream != null) ();
          if (rsp != null) ();
        }
      }
      else
      {
        //Create a request        HttpWebRequest request = (HttpWebRequest)(url + "?" + BuildQuery(parameters, "utf8"));
 
        //GET request         = "GET";
         = 5000;
         = "text/html;charset=UTF-8";
        HttpWebResponse response = (HttpWebResponse)();
        Stream myResponseStream = ();
        StreamReader myStreamReader = new StreamReader(myResponseStream, ("utf-8"));
 
        //Return to content        string retString = ();
        return retString;
      }
    }
 
    /// &lt;summary&gt;
    /// Assemble normal text request parameters.    /// &lt;/summary&gt;
    /// <param name="parameters">Key-Value form request parameter dictionary</param>    /// <returns>Request data after URL encoding</returns>    static string BuildQuery(IDictionary&lt;string, string&gt; parameters, string encode)
    {
      StringBuilder postData = new StringBuilder();
      bool hasParam = false;
      IEnumerator&lt;KeyValuePair&lt;string, string&gt;&gt; dem = ();
      while (())
      {
        string name = ;
        string value = ;
        // Ignore parameters with parameter names or parameter values ​​with empty parameters        if (!(name))//&amp;&amp; !(value)
        {
          if (hasParam)
          {
            ("&amp;");
          }
          (name);
          ("=");
          if (encode == "gb2312")
          {
            ((value, ("gb2312")));
          }
          else if (encode == "utf8")
          {
            ((value, Encoding.UTF8));
          }
          else
          {
            (value);
          }
          hasParam = true;
        }
      }
      return ();
    }
 
    /// &lt;summary&gt;
    /// Convert the response stream to text.    /// &lt;/summary&gt;
    /// <param name="rsp">Response stream object</param>    /// <param name="encoding">Encoding method</param>    /// <returns>Response text</returns>    static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
    {
       stream = null;
      StreamReader reader = null;
      try
      {
        // Read HTTP response in a character stream        stream = ();
        reader = new StreamReader(stream, encoding);
        return ();
      }
      finally
      {
        // Free up resources        if (reader != null) ();
        if (stream != null) ();
        if (rsp != null) ();
      }
    }
  }
}

The above is all about this article, I hope it will be helpful for everyone to learn C# programming.