2016-06-02 14 views

cevap

14

İşte bir cevap here dayanmaktadır.

#r "System.Web" 

using System.Net; 
using System.Web; 

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log) 
{ 
    string clientIP = ((HttpContextWrapper)req.Properties["MS_HttpContext"]).Request.UserHostAddress; 
    return req.CreateResponse(HttpStatusCode.OK, $"The client IP is {clientIP}"); 
} 
5

bu işlevi kullanmalısınız Get the IP address of the remote host

yerel request.Properties [RemoteEndpointMessageProperty.Name] masmavi

kullanılamaz önceden derlenmiş işlevleri hata ayıklama eğer [ "MS_HttpContext"] kullanılamaz

request.Properties

private string GetClientIp(HttpRequestMessage request) 
{ 
    if (request.Properties.ContainsKey("MS_HttpContext")) 
    { 
     return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress; 
    } 

    if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) 
    { 
     RemoteEndpointMessageProperty prop; 
     prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]; 
     return prop.Address; 
    } 

    return null; 
}