基于C#實(shí)現(xiàn)手機(jī)號(hào)碼歸屬地接口調(diào)用
本文實(shí)例介紹了手機(jī)號(hào)碼歸屬地接口調(diào)用基于C#實(shí)現(xiàn),分享給大家供大家參考,具體內(nèi)容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Xfrog.Net;
using System.Diagnostics;
using System.Web;
//----------------------------------
// 手機(jī)號(hào)碼歸屬地調(diào)用示例代碼 - 聚合數(shù)據(jù)
// 在線接口文檔:http://www.juhe.cn/docs/11
// 代碼中JsonObject類下載地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
namespace ConsoleAPI
{
class Program
{
static void Main(string[] args)
{
string appkey = "*******************"; //配置您申請(qǐng)的appkey
//1.手機(jī)歸屬地查詢
string url1 = "http://apis.juhe.cn/mobile/get";
var parameters1 = new Dictionary<string, string>();
parameters1.Add("phone" , ""); //需要查詢的手機(jī)號(hào)碼或手機(jī)號(hào)碼前7位
parameters1.Add("key", appkey);//你申請(qǐng)的key
parameters1.Add("dtype" , ""); //返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
string result1 = sendPost(url1, parameters1, "get");
JsonObject newObj1 = new JsonObject(result1);
String errorCode1 = newObj1["error_code"].Value;
if (errorCode1 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj1);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj1["error_code"].Value+":"+newObj1["reason"].Value);
}
}
/// <summary>
/// Http (GET/POST)
/// </summary>
/// <param name="url">請(qǐng)求URL</param>
/// <param name="parameters">請(qǐng)求參數(shù)</param>
/// <param name="method">請(qǐng)求方法</param>
/// <returns>響應(yīng)內(nèi)容</returns>
static string sendPost(string url, IDictionary<string, string> parameters, string method)
{
if (method.ToLower() == "post")
{
HttpWebRequest req = null;
HttpWebResponse rsp = null;
System.IO.Stream reqStream = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = method;
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
req.Timeout = 5000;
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
if (reqStream != null) reqStream.Close();
if (rsp != null) rsp.Close();
}
}
else
{
//創(chuàng)建請(qǐng)求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));
//GET請(qǐng)求
request.Method = "GET";
request.ReadWriteTimeout = 5000;
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
//返回內(nèi)容
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
/// <summary>
/// 組裝普通文本請(qǐng)求參數(shù)。
/// </summary>
/// <param name="parameters">Key-Value形式請(qǐng)求參數(shù)字典</param>
/// <returns>URL編碼后的請(qǐng)求數(shù)據(jù)</returns>
static string BuildQuery(IDictionary<string, string> parameters, string encode)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略參數(shù)名或參數(shù)值為空的參數(shù)
if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
if (encode == "gb2312")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
}
else if (encode == "utf8")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
}
else
{
postData.Append(value);
}
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 把響應(yīng)流轉(zhuǎn)換為文本。
/// </summary>
/// <param name="rsp">響應(yīng)流對(duì)象</param>
/// <param name="encoding">編碼方式</param>
/// <returns>響應(yīng)文本</returns>
static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
System.IO.Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式讀取HTTP響應(yīng)
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding);
return reader.ReadToEnd();
}
finally
{
// 釋放資源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)C#程序設(shè)計(jì)有所幫助。
- C#微信開發(fā)之獲取接口調(diào)用憑據(jù)
- 總結(jié)C#動(dòng)態(tài)調(diào)用WCF接口的兩種方法
- 基于C#的電視臺(tái)節(jié)目表接口調(diào)用代碼
- C#動(dòng)態(tài)webservice調(diào)用接口
- C#實(shí)現(xiàn)快遞api接口調(diào)用方法
- C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
- C#接口(Interface)用法分析
- c# 實(shí)現(xiàn)IComparable、IComparer接口、Comparer類的詳解
- C#抽象類和接口的區(qū)別分析
- C#中接口(interface)的理解
- c#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
- C#接口在派生類和外部類中的調(diào)用方法示例
相關(guān)文章
C#利用ASP.NET?Core開發(fā)學(xué)生管理系統(tǒng)詳解
隨著技術(shù)的進(jìn)步,跨平臺(tái)開發(fā)已經(jīng)成為了標(biāo)配,在此大背景下,ASP.NET?Core也應(yīng)運(yùn)而生。本文主要利用ASP.NET?Core開發(fā)一個(gè)學(xué)生管理系統(tǒng),感興趣的可以學(xué)習(xí)一下2022-01-01
C#實(shí)現(xiàn)Array添加擴(kuò)展實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)Array添加擴(kuò)展,對(duì)C#初學(xué)者有不錯(cuò)的參考價(jià)值,需要的朋友可以參考下2014-08-08
Unity實(shí)現(xiàn)仿3D輪轉(zhuǎn)圖效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)仿3D輪轉(zhuǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例
本文主要介紹了C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息
我們應(yīng)該都知道System.Management提供的類可以用于讀取本地計(jì)算機(jī)設(shè)備的各種數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息的相關(guān)資料,需要的朋友可以參考下2022-12-12
C# SqlSugar批量執(zhí)行SQL語句及批量更新實(shí)體對(duì)象的操作方法
SqlSugar 是一款 老牌 .NET開源ORM框架,由果糖大數(shù)據(jù)科技團(tuán)隊(duì)維護(hù)和更新 ,開箱即用最易上手的ORM,這篇文章主要介紹了C# SqlSugar批量執(zhí)行SQL語句以及批量更新實(shí)體對(duì)象,需要的朋友可以參考下2024-07-07

