webservice中如何获取客户端的IP


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,肯定是得不到的)

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM