基于C#實現(xiàn)手機號碼歸屬地接口調(diào)用
本文實例介紹了手機號碼歸屬地接口調(diào)用基于C#實現(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; //---------------------------------- // 手機號碼歸屬地調(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 = "*******************"; //配置您申請的appkey //1.手機歸屬地查詢 string url1 = "http://apis.juhe.cn/mobile/get"; var parameters1 = new Dictionary<string, string>(); parameters1.Add("phone" , ""); //需要查詢的手機號碼或手機號碼前7位 parameters1.Add("key", appkey);//你申請的key parameters1.Add("dtype" , ""); //返回數(shù)據(jù)的格式,xml或json,默認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">請求URL</param> /// <param name="parameters">請求參數(shù)</param> /// <param name="method">請求方法</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)建請求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8")); //GET請求 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> /// 組裝普通文本請求參數(shù)。 /// </summary> /// <param name="parameters">Key-Value形式請求參數(shù)字典</param> /// <returns>URL編碼后的請求數(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)流對象</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(); } } } }
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)C#程序設(shè)計有所幫助。
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery