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 Not 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>