快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

下载和记娱乐app:C后台调用跨域MVC服务并实现Cookie验证



背景

跟着富客户端框架的流行,以及浩繁优秀的前端js框架,很多环境我们会碰到跨域的问题,而js的ajax哀求是不容许直接跨域造访的,当然你会说可以用JSONP等,然则因为代码洁癖,不想在前端和后台添加callback,而且很多环境你是无法节制的,必要牵连斟酌太多的环境。

以是我直接绕过了,每个前端利用,自带一个通用后端办事代理,该办事办理跨域问题,自动代理帮前台获取跨域的数据。

若何算跨域

虽然是个老问题,然则照样要提醒留意下两点:同IP,不合端口下载和记娱乐app,数据造访是跨域的,然则Cookie造访是可以的(这个让我很难理解)

办理,源码

CookieContainer cookieContainer = new CookieContainer();

[HttpPost]

public string CommonPost(string url){

log.Info(C下载和记娱乐appookieHelper.GetCookie("ITDC_UserName") + "进入措施CommonPost Url=" + url);Uri address = new Uri(System.Configuration.ConfigurationManager.AppSettings["RESTfulAPI"].ToString() + url);

HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";       //远程办事,必要加入cookie验证

cookieContainer.Add(address, GetCookie("ITDC_UserName"));cookieContainer.Add(address, GetCookie("ITDC_UserRole"));

request.CookieContainer = cookieContainer;String下载和记娱乐appBuilder data = new StringBuilder();

for (int i = 0; i{

if (Request.QueryString.Keys[i].ToString() == "url") continue;data.Append("&" + Request.QueryString.Keys[i].ToString() + "=" + Request.QueryString[i].ToString());

}// Create a byte array of the data we want to send

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString().TrimStart('&'));// Set the content length in the request headers

request.ContentLength = byteData.Length;// Write data

using (Stream postStream = request.GetRequestStream()){

postStream.Write(byteData, 0, byteData.Length);}

st下载和记娱乐appring result = "";using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

{StreamReader reader = new StreamReader(response.GetResponseStream());

result = reader.ReadToEnd();}

log.Info(CookieHelper.GetCookie("ITDC_UserName") + " 履行完成 CommonPost Url=" + url);return (result);

}

前台调用

Ext.Ajax.request({url: APIUrl + '/Nebula/CommonPost?url=/Nebula/PostComment/&KlId=1&Msg=ok&Author下载和记娱乐app=admin&Title=文章标题',

method: "POST",success: function (response) {

Ext.Viewport.unmask();var obj = Ext.decode(response.responseText);

Ext.Msg.alert("提示", obj.Msg, Ext.emptyFn);},

failure: function (response) {Ext.Viewport.unmask();

Ext.Msg.alert("提示", "操作掉败,请反省收集!", Ext.emptyFn);}

});

您可能还会对下面的文章感兴趣: