Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML data sent through HttpWebRequest in vb.net is empty when it gets to php's $_POST
    primarykey
    data
    text
    <p>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.</p> <p>So I built a very quick &amp; 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 <code>&amp;lt;p&amp;gt;</code>. That same field, if it doesn't contain an HTML tag, will work fine and the data is passed on to the PHP page.</p> <p>This is the proxy code I'm using:</p> <pre><code>&lt;%@ Page Language="VB" %&gt; &lt;%@ Import Namespace="System.IO" %&gt; &lt;%@ Import Namespace="System.Net" %&gt; &lt;script runat="server"&gt; 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) &lt;&gt; "__" Then displayValues.Append("&amp;" &amp; 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 &lt;/script&gt; </code></pre> <p>Oh the PHP side, all I'm doing is <code>$var = $_POST["htmldata"];</code> , and <code>$var</code> is always empty. I'd give an example of my PHP but I don't know that it's necerssary. <code>$_POST["action"]</code>, for example, contains the correct action I'm expecting, so it's not the PHP code itself that's wrong.</p> <p>Does anyone have any idea what is happening here?</p> <p>=== EDIT ===</p> <p>Well, if you encode the HTML so html entities are correctly passed on (not as <code>&amp;lt;</code>), it works!</p> <p>Let me call it: n00b! :P</p> <p>(Fixed code)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload