据我所知,跨域发送 cookie 是不可能的(据我所知,浏览器出于隐私原因会阻止它们)。不过我希望有人能告诉我解决方法。
我已经在我们的 .Net winForms 客户端中实现了这一点,但是我无法让它在我们的网络软件上运行。
场景:我有我的网站,这需要调用一个第三方系统,该系统使用 XML 的其余实现,该实现存在于客户防火墙内部,并且无法从其办公室外部访问(VPN 也不是一个选项) .
我设置了我的cookie:
$.cookie('name', 'value', {path : '/', domain : '192.168.254.164'});
并发出发布请求
$.post(call, function(d) {}, 'text')
.success(function(d) {... /*do something*/
但是,cookie 并未发送(请参阅下面的请求和响应 header )
Request Headers Accept text/plain, */*; q=0.01 Accept-Encoding gzip, deflate Accept-Language en-gb,en;q=0.5 Connection keep-alive Host 192.168.254.164:8080 Origin http://localhost:27249 Referer http://localhost:27249/index.html User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0 Response Headers Access-Control-Allow-Orig... http://localhost:27249 Cache-Control no-cache Content-Type application/xml;charset=UTF-8 Date Tue, 01 May 2012 15:14:58 GMT Server Sun Java System Application Server 9.1_01 Set-Cookie JSESSIONID=8f7fbcd40348c0b5d912830bca8a; Path=/App Transfer-Encoding chunked X-Powered-By Servlet/2.5
我收到的响应告诉我,我未经授权(这是因为未设置 cookie)。
我无法修改第 3 方服务器上的任何内容,因此在代理服务器上,我无法使用 JSONP,因为所有内容都在 XML 中。
为了调试,我尝试在发送之前读取 cookie,但结果是“null”:
alert($.cookie(_svcMnuCookieSessionIdKey));
我是网络开发新手,所以这可能是一个奇怪的问题 - 但正如你从响应 header 中看到的那样,我相信我正在收到一个 cookie(我登录时收到相同的 cookie) - 与浏览器相同处理 cookie,它不应该保存它并自动将其应用到我的请求吗?而不是我必须像上面那样手动添加它?尽管如此,JSESSIONID 看起来在两个请求中都有不同的值,而且规范规定我必须使用登录时获得的原始 JSESSIONID 作为 cookie 的值。
在我的 .Net 应用程序中,我的做法与此类似:
Dim httpWebRequest As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://192.168.254.164:8080/App/RestfulService"), Net.HttpWebRequest)
httpWebRequest.UserAgent = My.Application.Info.AssemblyName
httpWebRequest.KeepAlive = True 'set the connection keep-alive (this is the default)
httpWebRequest.Headers.Set(Net.HttpRequestHeader.Pragma, "no-cache") 'we don't want caching to take place so we need to set the pragma header to say we don't want caching
httpWebRequest.Method = "POST"
If Not String.IsNullOrEmpty(_sessionId) Then 'it isn't used on some request, so don't add it if it is nothing
Dim cookie As New System.Net.Cookie("JSESSIONID", _sessionId)
cookie.Domain = New Uri("http://192.168.254.164:8080/App/RestfulService").Host
httpWebRequest.CookieContainer = New Net.CookieContainer
httpWebRequest.CookieContainer.Add(cookie)
End If
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim postData As Byte() = New System.Text.UTF8Encoding(False).GetBytes(RichTextBox1.Text)
httpWebRequest.ContentLength = postData.Length
Using tmpStream As IO.Stream = httpWebRequest.GetRequestStream
tmpStream.Write(postData, 0, postData.Length)
End Using
Dim httpWebResponse As Net.HttpWebResponse = CType(httpWebRequest.GetResponse, Net.HttpWebResponse)
If WebValidation.WebResponseHasContent(httpWebResponse) Then
Dim str As String = String.Empty
'Read the raw HTML from the request
Using sr As New IO.StreamReader(httpWebResponse.GetResponseStream, System.Text.Encoding.UTF8)
str = sr.ReadToEnd
End Using
End If
Return str
那么,这是一个跨域的cookie请求吗?以及如何在我的网络客户端中重新创建此功能?
最佳答案
我认为您并没有尝试设置跨域 cookie。系统不会要求你这样做。通常在这种情况下,您需要与第 3 方系统进行握手以获取 token ,或者在您的情况下首先获取 JSESSIONID(从系统)。然而,从表面上看,您正在尝试在第 3 方系统上设置 cookie,而不是接受来自系统的 cookie。那有意义吗?因此,我期望的是,您需要向第 3 方系统发出请求以获取 token ,然后在尝试获取 xml 时将该 token 传回给每个后续请求。
我认为在您的场景中您有网络应用程序和第 3 方应用程序。您可能会感到困惑的是在您的浏览器上设置的 cookie 与在您的网络应用程序上“设置”的 cookie(用于身份验证目的)。
当您通过第 3 方系统进行身份验证并返回 JSESSIONID 时,请尝试将后续请求 header 设置为包含“cookie” header ,其中包含 JSESSIONID 或第 3 方系统进行身份验证所需的任何内容。
请记住,在这种情况下,如果您从网络应用程序将第 3 方作为服务进行调用,则您的网络应用程序就是该第 3 方调用的“浏览器”。因此,当它对第 3 方系统进行后续调用时,它必须通过假装存在 cookie(包含您的身份验证 session ID)来模仿浏览器 - 您可以通过设置“cookie”请求 header 来做到这一点。
关于jquery - 如何在我的 Web 客户端上复制此 cookie 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400064/