webservice中如何获取客户端的IP

本文介绍如何通过this.Context.Request.UserHostAddress属性获取调用Web Service客户端的IP地址,并展示了一个简单的示例,包括部署和调用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

this.Context.Request.UserHostAddress;

很简单,就这么一个属性而已,貌似应该跟普通的ASP.NET应该大同小异的

 ------------------------------------------------------------------------------------

回复: 

验证:

*.asmx的代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
    /// <summary>
    
/// WebService1 的摘要说明
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    
// [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            string ip =this.Context.Request.UserHostAddress;
            return ip;
        }
    } 

} 

 

 

我在一台远程服务器上部署了webservice  ,见截图 : 部署webservice的外网ip为  :  *.*.114.214  (截图没弄好,被遮盖了一点,可以图片右键,保存到本地或者新窗口看)

 

 

在IIS上部署了该项目 ,然后我在另一个网段的内网机器上,新生成的控制台项目,调用该webservice

 

 

 获取的地址,为我目前电脑的外网出口IP (调用方的内网IP,肯定是得不到的)

 

 

 

 

转载于:https://www.cnblogs.com/davytitan/archive/2012/09/13/2682852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值