根據(jù)IP地址或者域名,查詢?cè)?/span>IP所屬的區(qū)域
1.
查詢IP信息
注意,該示例代碼僅適用于 www. 網(wǎng)站下API 使用該產(chǎn)品前,您需要通過 https://www./#/api/detail/?productID=118
申請(qǐng)API服務(wù)
1.查詢IP信息
using System;
using
System.Collections.Generic;
using
System.IO;
using
System.Net;
using
System.Text;
using
System.Web.Script.Serialization;
namespace
apishop_sdk
{
class Program
{
/**
* 轉(zhuǎn)發(fā)請(qǐng)求到目的主機(jī)
* @param method string 請(qǐng)求方法
* @param url string 請(qǐng)求地址
* @param params Dictionary<string,string> 請(qǐng)求參數(shù)
* @param headers Dictionary<string,string> 請(qǐng)求頭
* @return string
**/
static string apishop_send_request(string method, string url,
Dictionary<string, string> param, Dictionary<string, string>
headers)
{
string result = string.Empty;
try
{
string paramData = "";
if (param != null && param.Count > 0)
{
StringBuilder sbuilder = new StringBuilder();
foreach (var item in param)
{
if (sbuilder.Length > 0)
{
sbuilder.Append("&");
}
sbuilder.Append(item.Key + "=" + item.Value);
}
paramData = sbuilder.ToString();
}
method = method.ToUpper();
if (method == "GET")
{
url = string.Format("{0}?{1}", url, paramData);
}
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
if (method == "GET")
{
wbRequest.Method = "GET";
}
else if (method == "POST")
{
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
}
HttpWebResponse wbResponse =
(HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch
{
return "";
}
return result;
}
class Response
{
public string statusCode;
}
static void Main(string[] args)
{
string method = "POST";
string url = "https://api./common/ip/queryIPInfo";
Dictionary<string, string> param = new Dictionary<string,
string>();
param.Add("ip", ""); //ip地址
Dictionary<string, string> headers = null;
string result = apishop_send_request(method, url, param, headers);
if (result == "")
{
//返回內(nèi)容異常,發(fā)送請(qǐng)求失敗
Console.WriteLine("發(fā)送請(qǐng)求失敗");
return;
}
Response res = new JavaScriptSerializer().Deserialize<Response>(result);
if (res.statusCode == "000000")
{
//狀態(tài)碼為000000, 說明請(qǐng)求成功
Console.WriteLine(string.Format("請(qǐng)求成功: {0}",
result));
}
else
{
//狀態(tài)碼非000000, 說明請(qǐng)求失敗
Console.WriteLine(string.Format("請(qǐng)求失敗: {0}",
result));
}
Console.ReadLine();
}
}
}
|