302重定向,MVC中的Get,Post请求。

1.在访问页遇到重定向,Get,Post跳转处理,在跳转后的页面获取访问端的IP,他们的IP是否发生变化。。。

2.重定向处理后获取的IP还是访问端IP,而用Get,Post请求处理后,获取的访问端IP则是处理页的IP。

3.获取客户端IP的代码

   public string GetClientIP()
{
HttpContext current = HttpContext.Current;
string userHostAddress = string.Empty;
if (current != null)
{
if ((current.Session != null) && (current.Session["cnki_sys_user_remote_addr"] != null))
{
userHostAddress = current.Session["cnki_sys_user_remote_addr"].ToString();
if (!string.IsNullOrEmpty(userHostAddress))
{
return userHostAddress;
}
}
userHostAddress = "127.0.0.1";
userHostAddress = current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(userHostAddress))
{
userHostAddress = current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(userHostAddress))
{
userHostAddress = current.Request.UserHostAddress;
}
}
else
{
string[] strArray = userHostAddress.Split(new char[] { ',' });
if ((strArray.Length > ) && !string.IsNullOrEmpty(strArray[strArray.Length - ]))
{
userHostAddress = strArray[strArray.Length - ];
}
}
if (current.Session != null)
{
current.Session["cnki_sys_user_remote_addr"] = userHostAddress;
}
}
return userHostAddress;
}

4.向mvcapi中发出的Post和Get请求,以及返回信息的接收和处理代码。

 public class ECPManage
{
public Result GetResultFromConfig(string config, params string[] para)//config是mvcapi的处理路径,para是参数。
{
string getUidUrl = string.Format(ConfigurationManager.AppSettings[config], para);
string retJSON = SendGet(getUidUrl).TrimStart("?(".ToCharArray()).TrimEnd(')');
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(retJSON)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Result));
Result t = (Result)serializer.ReadObject(ms);
return t;
}
}
/// <summary>
/// 发送post请求,返回信息
/// </summary>
/// <param name="uri"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public string SendPost(string uri, string data, string encoding = "utf8")
{
using (WebClient wCient = new WebClient())
{
if ("utf8" == encoding)
{
wCient.Encoding = Encoding.UTF8;
}
else
{
wCient.Encoding = Encoding.Default;
}
return wCient.UploadString(uri, data);
}
}
/// <summary>
/// 发送get请求,返回信息
/// </summary>
/// <param name="uri"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public string SendGet(string uri, string encoding = "utf8")
{
using (WebClient wCient = new WebClient())
{
if ("utf8" == encoding)
{
wCient.Encoding = Encoding.UTF8;
}
else
{
wCient.Encoding = Encoding.Default;
}
return wCient.DownloadString(uri);
}
}
}

302重定向,MVC中的Get,Post请求。

上一篇:【BZOJ 2004】: [Hnoi2010]Bus 公交线路


下一篇:Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持