开发者

Can't send cookies from vb.net page to php backend

I'm trying to create a proxy for cross-domain AJAX POST queries and, in order to do that, I need to be adding the cookies from my client (browser)'s request into the POST request I send to the PHP backend, which is using HttpWebRequest. In order to do this, I'm adding each of the Request.Cookie into the HttpWebRequest.CookieContainer object. For some reason, none of the cookies I add to it ever get to my PHP backend.

I understand from some searches that the CookieContainer object is URI-dependent (I'm not sure how to even explain it!), so perhaps that's the cause of my issues. However, I've tried setting the domain on my cookie to different things and it doesn't seem to affect it. So maybe URI means something different than the Cookie's Domain parameter. If that's the case, I'm stuck.

I'm all for an alternate solution if it exists (if there's a way to do an automatic cross-domain proxy in asp.net without the need for a plugin, that'll save my life!).

Here's the code I'm using.

<%@ Page Language="VB" validateRequest="false" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If N开发者_Go百科ot Page.IsPostBack Then
            Dim displayValues As New StringBuilder()
            Dim CookieJar as New CookieContainer
            Dim objCookieColl as HttpCookieCollection
            Dim objThisCookie As HttpCookie
            Dim myCookie As HttpCookie
            Dim arr1() As String
            Dim myCount as Integer

            objCookieColl = Request.Cookies
            arr1 = objCookieColl.AllKeys
            Dim postedValues As NameValueCollection = Request.Form
            Dim nextKey As String
            For i As Integer = 0 To postedValues.AllKeys.Length - 1
                nextKey = postedValues.AllKeys(i)
                If nextKey.Substring(0, 2) <> "__" Then
                    displayValues.Append("&" & nextKey)
                    displayValues.Append("=")
                    displayValues.Append(Server.UrlEncode(postedValues(i)))
                End If
            Next

            Dim uri As New Uri("http://www.otherdomain.com/subfolder/backend.php")
            Dim data As String = displayValues.ToString
            If (uri.Scheme = uri.UriSchemeHttp) Then
                Dim postRequest As HttpWebRequest = HttpWebRequest.Create(uri)
            For index As Integer = 1 to UBound(arr1)
                objThisCookie = objCookieColl(arr1(index))
                Dim tempCookie As New Cookie(objThisCookie.Name, objThisCookie.Value, objThisCookie.Path, Request.ServerVariables("HTTP_HOST"))
                myCount = myCount + 1
                CookieJar.Add(tempCookie)
            Next
                postRequest.CookieContainer = CookieJar
                postRequest.Method = Request.HttpMethod
                postRequest.ContentLength = data.Length
                postRequest.ContentType = "application/x-www-form-urlencoded"
                Dim writer As New StreamWriter(postRequest.GetRequestStream())
                writer.Write(data)
                writer.Close()

                Dim myResponse As HttpWebResponse = postRequest.GetResponse()
                Dim x As Integer
                While x < myResponse.Headers.Count
                    Response.AppendHeader(myResponse.Headers.Keys(x), myResponse.Headers(x))
                    x = x + 1
                End While
                Dim reader As New StreamReader(myResponse.GetResponseStream())
                Dim responseString As String = reader.ReadToEnd()
                myResponse.Close()
                Response.Write(responseString)

            End If
        End If
    End Sub

</script>


I'm not familiar with how ASP sets, stores, and handles cookies. But on your PHP backend try

<?php
echo "<pre>";
print_r($_COOKIE);
echo "</pre>";
?>

this way you can see if the PHP is even finding any cookies that remotely look like the cookie your trying to read. I do have to say though, mixing languages like this is not something I would advise, though I am sure there is decent logic behind this need. May I suggest a possible alternative. Bridge the gap with maybe some JavaScript. Set your variables up like you want them stored in the cookie, then use JavaScript to Set a cookie. http://www.w3schools.com/JS/js_cookies.asp for reference of how to set up a cookie via JavaScript I know with PHP I can retieve a cookie I set with JavaScript so hopefully this helps out some, and what I know of ASP you should be able to use JavaScript there as well.

Grant it this isn't an end all be all solution, may not be the best either. I wouldn't use this concept on a lot of things you may be trying to connect the front and the back with but I can't see to much harm with one to another.


You mean that you have a site that is run using .Net (asp in your situation) and then you want to pass the cookie on to another application running PHP? I think the cookies are prevented from being shared with another "site" since this is not supposed to be done. It is often used as an attack vector of hackers and thus I would be that you server is trying to prevent it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜