本地應用 (WinForm/Web) → HTTP GET 請求第三方 API (showji.com) → 解析 XML 回應 → 顯示省份、城市、運營商、區號、郵編。
using System;
using System.Xml;
using System.Collections.Generic;
public static class MobileLocator
{
private const string ApiUrl = "https://api.showji.com/Locating/default.aspx?m={0}";
///
/// 查詢手機號碼歸屬地
///
/// 成功回傳陣列 [號碼, 省份, 城市, 區號, 郵編, 卡類型, 運營商],失敗回傳 null
public static string[] Query(string mobile)
{
if (string.IsNullOrWhiteSpace(mobile) || mobile.Length != 11)
return null;
try
{
var doc = new XmlDocument();
doc.Load(string.Format(ApiUrl, mobile)); // 同步阻塞,Web 環境建議改非同步
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "https://api.showji.com/Locating/");
// XPath 查詢各節點
var queryResult = doc.SelectSingleNode("//ns:QueryResult", nsmgr)?.InnerText;
if (queryResult != "True") return null; // 查詢失敗或號碼無效
return new string[] {
doc.SelectSingleNode("//ns:Mobile", nsmgr)?.InnerText, // 號碼
doc.SelectSingleNode("//ns:Province", nsmgr)?.InnerText, // 省份
doc.SelectSingleNode("//ns:City", nsmgr)?.InnerText, // 城市
doc.SelectSingleNode("//ns:AreaCode", nsmgr)?.InnerText, // 區號
doc.SelectSingleNode("//ns:PostCode", nsmgr)?.InnerText, // 郵編
doc.SelectSingleNode("//ns:Card", nsmgr)?.InnerText, // 卡類型
doc.SelectSingleNode("//ns:Corp", nsmgr)?.InnerText // 運營商
};
}
catch (Exception ex)
{
// 記錄日誌: ex.Message, ex.StackTrace
return null;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MobileQuery.aspx.cs" Inherits="MobileQuery" %>
<!DOCTYPE html>
<html><head><title>手機歸屬地查詢</title>
<style>
.query-box { max-width: 400px; margin: 50px auto; padding: 20px; border: 1px solid #ddd; border-radius: 8px; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
input[type=text] { width: 100%; padding: 10px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; }
button { width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; }
button:hover { background: #0056b3; }
.result { margin-top: 20px; padding: 15px; background: #f8f9fa; border-radius: 4px; display: none; }
.result-item { margin: 8px 0; }
.label { font-weight: bold; color: #333; }
.error { color: #dc3545; text-align: center; margin-top: 10px; display: none; }
</style>
</head><body>
<div class="query-box">
<h2>手機號碼歸屬地查詢</h2>
<div class="form-group">
<label>輸入 11 位手機號碼</label>
<asp:TextBox ID="txtMobile" runat="server" MaxLength="11" />
</div>
<asp:Button ID="btnQuery" runat="server" Text="查詢" OnClick="btnQuery_Click" CssClass="btn" />
<div class="error" id="errMsg" runat="server"></div>
<div class="result" id="divResult" runat="server">
<div class="result-item"><span class="label">號碼:</span><span id="lblMobile" runat="server"></span></div>
<div class="result-item"><span class="label">省份:</span><span id="lblProvince" runat="server"></span></div>
<div class="result-item"><span class="label">城市:</span><span id="lblCity" runat="server"></span></div>
<div class="result-item"><span class="label">區號:</span><span id="lblAreaCode" runat="server"></span></div>
<div class="result-item"><span class="label">郵編:</span><span id="lblPostCode" runat="server"></span></div>
<div class="result-item"><span class="label">卡類型:</span><span id="lblCard" runat="server"></span></div>
<div class="result-item"><span class="label">運營商:</span><span id="lblCorp" runat="server"></span></div>
</div>
</div>
</body></html>
public partial class MobileQuery : System.Web.UI.Page
{
protected void btnQuery_Click(object sender, EventArgs e)
{
string mobile = txtMobile.Text.Trim();
divResult.Style["display"] = "none";
errMsg.Style["display"] = "none";
errMsg.InnerText = "";
if (string.IsNullOrEmpty(mobile) || mobile.Length != 11 || !long.TryParse(mobile, out _))
{
errMsg.InnerText = "請輸入正確的 11 位手機號碼";
errMsg.Style["display"] = "block";
return;
}
var result = MobileLocator.Query(mobile);
if (result != null)
{
lblMobile.InnerText = result[0];
lblProvince.InnerText = result[1];
lblCity.InnerText = result[2];
lblAreaCode.InnerText = result[3];
lblPostCode.InnerText = result[4];
lblCard.InnerText = result[5];
lblCorp.InnerText = result[6];
divResult.Style["display"] = "block";
}
else
{
errMsg.InnerText = "查詢失敗:號碼不存在、API 異常或網路錯誤";
errMsg.Style["display"] = "block";
}
}
}
public class MobileLocatorAsync
{
private static readonly HttpClient _http = new HttpClient();
private const string ApiUrl = "https://api.showji.com/Locating/default.aspx?m={0}";
public static async Task QueryAsync(string mobile)
{
if (string.IsNullOrWhiteSpace(mobile) || mobile.Length != 11) return null;
try
{
string url = string.Format(ApiUrl, mobile);
string xml = await _http.GetStringAsync(url);
var doc = new XmlDocument();
doc.LoadXml(xml);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "https://api.showji.com/Locating/");
if (doc.SelectSingleNode("//ns:QueryResult", nsmgr)?.InnerText != "True") return null;
return new MobileInfo {
Mobile = doc.SelectSingleNode("//ns:Mobile", nsmgr)?.InnerText,
Province = doc.SelectSingleNode("//ns:Province", nsmgr)?.InnerText,
City = doc.SelectSingleNode("//ns:City", nsmgr)?.InnerText,
AreaCode = doc.SelectSingleNode("//ns:AreaCode", nsmgr)?.InnerText,
PostCode = doc.SelectSingleNode("//ns:PostCode", nsmgr)?.InnerText,
CardType = doc.SelectSingleNode("//ns:Card", nsmgr)?.InnerText,
Carrier = doc.SelectSingleNode("//ns:Corp", nsmgr)?.InnerText
};
}
catch { return null; }
}
}
public class MobileInfo
{
public string Mobile { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string AreaCode { get; set; }
public string PostCode { get; set; }
public string CardType { get; set; }
public string Carrier { get; set; }
}
掌握外部 API 呼叫、XML 解析、非同步模式、快取與降級策略,是構建穩健整合服務的關鍵能力。