0

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 页面而不是重定向到该页面的问题?

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

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

4

1 回答 1

0

为了说明我如何向网站发布数据,用户登录保存在请求和请求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();
于 2013-07-15T18:15:02.387 回答