-1

我正在使用此代码根据 IP 地址检索用户位置。它不返回任何位置,国家代码......数据表中的所有列都是空白的,除了 IP 在此处输入图像描述

string i = "aa.b.23.58";
            DataTable dt = GetLocation(i.Trim());
     public DataTable GetLocation(string strIPAddress)
        {
            //Create a WebRequest with the current Ip
        WebRequest _objWebRequest =
            WebRequest.Create("http://freegeoip.appspot.com/xml/"  +strIPAddress);


        //Create a Web Proxy
        WebProxy _objWebProxy =
           new WebProxy("http://freegeoip.appspot.com/xml/"
                     + strIPAddress, true);

        //Assign the proxy to the WebRequest
        _objWebRequest.Proxy = _objWebProxy;

        //Set the timeout in Seconds for the WebRequest
        _objWebRequest.Timeout = 2000;

        try
        {
            //Get the WebResponse 
            WebResponse _objWebResponse = _objWebRequest.GetResponse();
            //Read the Response in a XMLTextReader
            XmlTextReader _objXmlTextReader
                = new XmlTextReader(_objWebResponse.GetResponseStream());

            //Create a new DataSet
            DataSet _objDataSet = new DataSet();
            //Read the Response into the DataSet
            _objDataSet.ReadXml(_objXmlTextReader);

            return _objDataSet.Tables[0];
        }
        catch
        {
            return null;
        }
    } // End of GetLocation 
4

1 回答 1

0

试试这个示例 http://www.codeproject.com/Articles/3265/GeoIP-Where-are-your-internet-visitors-coming-from

可能你对 freegeoip.appspot.com 有一些问题

您可以将 ip 保留到项目中的位置 db。

于 2012-10-20T16:02:31.770 回答