dsf55s1233 2011-05-02 17:41
浏览 57
已采纳

当它到达php的$ _POST时,通过vb.net中的HttpWebRequest发送的HTML数据是空的

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 &lt;), it works!

Let me call it: n00b! :P

(Fixed code)

  • 写回答

1条回答 默认 最新

  • doushui5587 2011-05-03 16:01
    关注

    After some research, it seems that I had forgotten to encode the HTML data with Server.URLEncode (the equivalent to encodeURI in javascript). That is the solution to this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛