【问题标题】:posting form to php page causes .php file download将表单发布到 php 页面会导致 .php 文件下载
【发布时间】:2013-07-15 17:13:38
【问题描述】:

C# 代码

Response.Clear();

string postbackUrl = "https://payeer.com/ajax/api/api.php";
string account = "620913";
string orderid = "77777";
string amount = Convert.ToDecimal(IncreaseUSDtxb.Text).ToString("N2");
string units = "USD";
string key = "test1";

StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");
sb.AppendFormat("<form name='form' action='{0}' method='post'>", postbackUrl);
sb.AppendFormat("<input type='hidden' name='m_shop' value='{0}'>", account);
sb.AppendFormat("<input type='hidden' name='m_orderid' value='{0}'>", orderid);
sb.AppendFormat("<input type='hidden' name='m_amount' value='{0}'>", amount);
sb.AppendFormat("<input type='hidden' name='m_curr' value='{0}'>", units);
sb.AppendFormat("<input type='hidden' name='m_key' value='{0}'>", key);
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");

Response.Write(sb.ToString());

Response.End();

通常,此代码允许我将数据发布到第三方网站,将用户重定向到它们并使用以下内容检索答案:

System.Collections.Specialized.NameValueCollection ReadForm = Request.Form;
yourvariable == ReadForm["requestedcolumn"]

当我在我的 postbackUrl 中使用“.asp”页面作为发布到的 url 时,会发生这种情况而不会出错。 但是,在这种情况下,上述代码会导致 api.php 被下载,尽管我可以在浏览器中打开 https://payeer.com/ajax/api/api.php 并给出一个空白页面,当然没有问题。

如何解决此代码下载 .php 页面而不是重定向到该页面的问题?

更新:已解决。显然,由于某种原因,如果您在 PostBackUrl 或没有“www”的表单操作中以其他正确格式指定链接,则在这种情况下,它会专门下载“.php”资源,而不是像包含“www”那样重定向到它。

我还不确定 post 操作是否在我将更新的代码中正常工作,尽管它应该在非 php 第三方资源的情况下正常工作。

【问题讨论】:

  • 我从左上角的 Stack Exchange 收件箱收到通知,当前帖子有一条评论,开头是“如果将 php 文件放在 Web 根目录中并导航到它会发生什么?微不足道的……”请问如何查看?
  • 我似乎无法在任何地方找到解决方案。 a href='' 和 Response.Redirect 工作正常,但 PostBackUrl='' 和 Form Action='' 会导致每个 php 文件都被下载,这可能是我无法解决的单独问题。

标签: c# asp.net forms post redirect


【解决方案1】:

为了说明我如何向网站发布数据,用户登录保存在请求和请求2之间:

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.website.net/login.php");
        request.Method = "POST";
        request.CookieContainer = new CookieContainer();
        byte[] query = Encoding.UTF8.GetBytes("uid=login&pwd=password");
        request.ContentLength = query.Length;
        request.ContentType = "application/x-www-form-urlencoded";
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(query,0, query.Length);
        }
        var response = request.GetResponse();
        var str = new StreamReader(response.GetResponseStream()).ReadToEnd();

        HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create("http://www.website.net/index.php");
        request2.Method = "GET";
        request2.CookieContainer = request.CookieContainer;//<-pass cookies
        var response2 = request2.GetResponse();
        var str2 = new StreamReader(response2.GetResponseStream()).ReadToEnd();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
相关资源
最近更新 更多