首页 新闻 会员 周边 捐助

Webapi 获取用户ip及地址

0
[已解决问题] 解决于 2019-10-26 21:38

我的webapi 需要获取用户的地址~~~ 这个接口没问题在api 中 调用了别人的api地址,返回的数据是这样的

但是我把我的api 发布到服务器,现在每次调用都是插入的服务器ip.....
请问如何解决?

永远跟党走i的主页 永远跟党走i | 小虾三级 | 园豆:1542
提问于:2019-10-26 17:00
< >
分享
最佳答案
0

代码中是如何获取IP地址的?

奖励园豆:5
dudu | 高人七级 |园豆:29732 | 2019-10-26 20:01

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Web;

namespace WebAPI5.IP
{
public class JsonData
{
public static string code; /自定义错误代码 类型如 :500 服务器错误 200 Success 404 类似错误代码/
public static string message; /自定义错误/
public static long time = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000); /* 时间戳*/

    /// <summary>
    /// 封装请求ip方法 Post
    /// </summary>
    /// <param name="url"></param>
    /// <param name="body"></param>
    /// <returns></returns>
    public static string HttpPost(string url, string body)
    {
        Encoding encoding = Encoding.UTF8;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.ContentType = "application/json";

        byte[] buffer = encoding.GetBytes(body);
        request.ContentLength = buffer.Length;
        request.GetRequestStream().Write(buffer, 0, buffer.Length);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }
    }
    /// <summary>
    /// 封装请求Ip方法 Get
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string HttpGet(string url)
    {
        Encoding encoding = Encoding.UTF8;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.ContentType = "application/json";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }
    }
    /// <summary>
    /// ip请求地址
    /// </summary>
    /// <returns></returns>
    public static string GetIp() {
        return HttpGet("https://apis.map.qq.com/ws/location/v1/ip?key=3BFBZ-ZKD3X-LW54A-ZT76D-E7AHO-4RBD5");
    }


    }


public class IP
{
    /// <summary>
    /// IP地址
    /// </summary>
    public string ip;
    /// <summary>
    /// 国家
    /// </summary>
    public string nation;
    /// <summary>
    /// 省份
    /// </summary>
    public string province;
    /// <summary>
    /// 城市
    /// </summary>
    public string city;
    /// <summary>
    /// 地区
    /// </summary>
    public string district;
    /// <summary>
    /// 邮编
    /// </summary>
    public string adcode;

    public string status;

    public string message;
    public string result;
   
    public string location;
    
    public string lat;
    
    public string lng;
    
    public string ad_info; 
}
 

}
调用:

        //string ip = JsonData.GetIp();

        //JArray jarry = JArray.Parse("[" + ip + "]");
        //SYS_LoginInfo loginInfo;
      
        //    loginInfo = new SYS_LoginInfo
        //    {
        //        SYS_LoginTime = Convert.ToInt32(JsonData.time),                               //登陆时间
        //        SYS_IP = jarry[0]["result"]["ip"].ToString(),                    //ip 
        //        SYS_city = jarry[0]["result"]["ad_info"]["city"].ToString(),      // 城市
        //        SYS_district = jarry[0]["result"]["ad_info"]["district"].ToString(),  //地区
        //        SYS_nation = jarry[0]["result"]["ad_info"]["nation"].ToString(),   // 国家
        //        SYS_province = jarry[0]["result"]["ad_info"]["province"].ToString()  //省份
        //    }

然后执行
string sql = "insert dataimg(imgdata,SYS_LoginTime,SYS_IP,SYS_city,SYS_district,SYS_nation,SYS_province) values(@imgdata,@SYS_LoginTime,@SYS_IP,@SYS_city,@SYS_district,@SYS_nation,@SYS_province)";
SqlParameter imgdata = new SqlParameter("@imgdata", loginInfo.info == null ? "" : loginInfo.info);
SqlParameter SYS_LoginTime = new SqlParameter("@SYS_LoginTime", Convert.ToInt32(JsonData.time)); ///时间
SqlParameter SYS_IP = new SqlParameter("@SYS_IP", "");
SqlParameter SYS_city = new SqlParameter("@SYS_city", loginInfo.SYS_city==null?"": loginInfo.SYS_city);
SqlParameter SYS_district = new SqlParameter("@SYS_district", loginInfo.SYS_district == null ? "" : loginInfo.SYS_district);
SqlParameter SYS_nation = new SqlParameter("@SYS_nation", loginInfo.SYS_nation == null ? "" : loginInfo.SYS_nation);
SqlParameter SYS_province = new SqlParameter("@SYS_province", loginInfo.SYS_province == null ? "" : loginInfo.SYS_province);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();

        cmd.Parameters.Add(imgdata);
        cmd.Parameters.Add(SYS_LoginTime);
        cmd.Parameters.Add(SYS_IP);
        cmd.Parameters.Add(SYS_city);
        cmd.Parameters.Add(SYS_district);
        cmd.Parameters.Add(SYS_nation);
        cmd.Parameters.Add(SYS_province);
        int count = cmd.ExecuteNonQuery();
        conn.Close();
        return count;

插入到数据 库 就变成 服务器ip地址了

永远跟党走i | 园豆:1542 (小虾三级) | 2019-10-26 20:46

@Cgrain: 上面的代码中并没有获取客户端 IP 地址的代码,比如

var ip = Request.UserHostAddress;
dudu | 园豆:29732 (高人七级) | 2019-10-26 21:08

@dudu: 我这样掉是不行嘛
public static string GetIp() {
return HttpGet("https://apis.map.qq.com/ws/location/v1/ip?key=3BFBZ-ZKD3X-LW54A-ZT76D-E7AHO-4RBD5");

有点蒙~~~

永远跟党走i | 园豆:1542 (小虾三级) | 2019-10-26 21:11

@Cgrain: 这是服务器自己请求 apis.map.qq.com ,得到的是服务器自己的 IP 地址

dudu | 园豆:29732 (高人七级) | 2019-10-26 21:23

@dudu: var ip = Request.UserHostAddress; 这样就是获取客户端的嘛?

永远跟党走i | 园豆:1542 (小虾三级) | 2019-10-26 21:25

@Cgrain: 是的

dudu | 园豆:29732 (高人七级) | 2019-10-26 21:33

@dudu: 还有这个省市区 怎么获取

永远跟党走i | 园豆:1542 (小虾三级) | 2019-10-26 21:38

@Cgrain:

var ip = Request.UserHostAddress;
var result = HttpGet("https://apis.map.qq.com/ws/location/v1/ip?ip=" + ip + "&key=3BFBZ-ZKD3X-LW54A-ZT76D-E7AHO-4RBD5"
dudu | 园豆:29732 (高人七级) | 2019-10-26 21:49

@dudu: 还可以这样.... 谢谢大佬!!!

永远跟党走i | 园豆:1542 (小虾三级) | 2019-10-26 21:59
其他回答(1)
0

是不是用代理模式运营的.在iis里应该不会这样

吴瑞祥 | 园豆:29449 (高人七级) | 2019-10-26 18:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册