本文整理汇总了C#中HttpContext.GetClientIpAddress方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContext.GetClientIpAddress方法的具体用法?C# HttpContext.GetClientIpAddress怎么用?C# HttpContext.GetClientIpAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpContext
的用法示例。
在下文中一共展示了HttpContext.GetClientIpAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Collect
public static RequestInfo Collect(HttpContext context, IEnumerable<string> exclusions) {
if (context == null)
return null;
var info = new RequestInfo {
ClientIpAddress = context.GetClientIpAddress(),
HttpMethod = context.Request.Method,
IsSecure = context.Request.IsHttps,
Path = context.Request.Path.HasValue ? context.Request.Path.Value : "/",
};
if (!String.IsNullOrEmpty(context.Request.Host.Host))
info.Host = context.Request.Host.Host;
if (context.Request.Host.Port.HasValue)
info.Port = context.Request.Host.Port.Value;
if (context.Request.Headers.ContainsKey(HeaderNames.UserAgent))
info.UserAgent = context.Request.Headers[HeaderNames.UserAgent].ToString();
if (context.Request.Headers.ContainsKey(HeaderNames.Referer))
info.Referrer = context.Request.Headers[HeaderNames.Referer].ToString();
var exclusionList = exclusions as string[] ?? exclusions.ToArray();
info.Cookies = context.Request.Cookies.ToDictionary(exclusionList);
info.QueryString = context.Request.Query.ToDictionary(exclusionList);
if (context.Request.HasFormContentType && context.Request.Form.Count > 0) {
info.PostData = context.Request.Form.ToDictionary(exclusionList);
} else if (context.Request.ContentLength.HasValue && context.Request.ContentLength.Value > 0) {
if (context.Request.ContentLength.Value < 1024 * 50) {
try {
if (context.Request.Body.Position > 0)
context.Request.Body.Position = 0;
using (var inputStream = new StreamReader(context.Request.Body))
info.PostData = inputStream.ReadToEnd();
} catch (Exception ex) {
info.PostData = "Error retrieving POST data: " + ex.Message;
}
} else {
string value = Math.Round(context.Request.ContentLength.Value / 1024m, 0).ToString("N0");
info.PostData = String.Format("Data is too large ({0}kb) to be included.", value);
}
}
return info;
}