打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
eoLinker-API Shop IP地址查询 C#调用示例代码

IP地址查询

根据IP地址或者域名,查询该IP所属的区域  

该产品拥有以下APIs

1.         查询IP信息

注意,该示例代码仅适用于 www.apishop.net 网站下API 使用该产品前,您需要通过 https://www.apishop.net/#/api/detail/?productID=118申请API服务


1.查询IP信息

using System;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Net;
usingSystem.Text;
usingSystem.Web.Script.Serialization;

namespaceapishop_sdk
{
   class Program
   {
       /**
        * 转发请求到目的主机
        * @param method string 请求方法
        * @param url string 请求地址
        * @param params Dictionary<string,string> 请求参数
        * @param headers Dictionary<string,string> 请求头
        * @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.apishop.net/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 == "")
           {
               //返回内容异常,发送请求失败
               Console.WriteLine("发送请求失败");
               return;
           }

           Response res = new JavaScriptSerializer().Deserialize<Response>(result);
           if (res.statusCode == "000000")
           {
               //状态码为000000, 说明请求成功
               Console.WriteLine(string.Format("请求成功: {0}",result));
           }
           else
           {
               //状态码非000000, 说明请求失败
               Console.WriteLine(string.Format("请求失败: {0}",result));
           }
           Console.ReadLine();
       }
   }
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
口袋通api对接
C# 服务端篇之实现RestFul Service开发(简单实用)
Customize summernote
『互联网架构』软件架构
C# 实现MD5加密
C#哈希Md5加密
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服