Background info: I was doing cross-domain ajax calls from an HTML-only (no server programming) page, to a PHP backend on a different domain. I realized at one point that I couldn't do POST with cross-domain, so I figured I needed a local proxy. Unfortunately, PHP is not available on that domain so I have to resort to ASP.NET.
So I built a very quick & dirty vb.net proxy... It works, except for one very important detail. If any of my form fields contain HTML, the data is not sent to PHP (or PHP doesn't receive it, I'm not sure). Actually, the $_POST variable exists, it's just empty all the time as soon as it contains what looks like html code, such as
<p>. That same field, if it doesn't contain an HTML tag, will work fine and the data is passed on to the PHP page.
This is the proxy code I'm using:
<%@ Page Language="VB" %> <%@ 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 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(postedValues(i)) displayValues.Append(Server.UrlEncode(postedValues(i))) End If Next ' here, both postedValues(2) and Request.Form("htmldata") contain the correct HTML data. Dim uri As New Uri("http://www.myotherdomain/folder/page.php") Dim data As String = displayValues.ToString 'Here, data contains the correct HTML code along with the other POST variables. If (uri.Scheme = uri.UriSchemeHttp) Then Dim request As HttpWebRequest = HttpWebRequest.Create(uri) request.Method = WebRequestMethods.Http.Post request.ContentLength = data.Length request.ContentType = "application/x-www-form-urlencoded" Dim writer As New StreamWriter(request.GetRequestStream()) writer.Write(data) writer.Close() Dim myResponse As HttpWebResponse = request.GetResponse() 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>
Oh the PHP side, all I'm doing is
$var = $_POST["htmldata"]; , and
$var is always empty. I'd give an example of my PHP but I don't know that it's necerssary.
$_POST["action"], for example, contains the correct action I'm expecting, so it's not the PHP code itself that's wrong.
Does anyone have any idea what is happening here?
=== EDIT ===
Well, if you encode the HTML so html entities are correctly passed on (not as
<), it works!
Let me call it: n00b! :P
- 当它到达php的$ _POST时，通过vb.net中的HttpWebRequest发送的HTML数据是空的
- 验证Curl HTTP Post
- 字符编码问题 - PHP输出，由.NET读取，通过HttpWebRequest