带有POST和GET的HttpWebRequest [英] HttpWebRequest with POST and GET at the same time

查看:90
本文介绍了带有POST和GET的HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将用户重定向到 http://www.someurl.com?id=2使用POST方法. 有可能吗?如果可以,那么如何?

I need to redirect a user to http://www.someurl.com?id=2 using a POST method. Is it possible? If yes, then how?

现在我已经关注了,它可以正确转发POST数据,但是它删除了?id = 2:

Right now I have following and it forwards the POST data properly, but it removes the ?id=2:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.someurl.com?id=2");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(postData);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Response.Write(reader.ReadToEnd());
}

我同时需要查询字符串数据->?id = 2和POST数据的原因是因为我将查询字符串传递到javascript将在其中处理查询字符串数据并且.NET将与数据一起使用的页面通过POST方法发送.我传递的POST数据可能长于GET方法允许的最大字符数,因此我不能仅使用GET方法...所以,您有什么建议?

The reason I need both query string data -> ?id=2 and POST data is because I pass the query string to page in which javascript is going to be handling the query string data and .NET is going to work with data sent via POST method. The POST data that I am passing can be longer than maximum amount of characters that GET method allows, therefore I can't use only GET method... so, what are your suggestions?

更多信息: 我正在编写一个路由页面,该页面将一些自定义信息添加到查询字符串,然后将所有旧的和新的数据进一步路由到所提供的某个URL.该页面应该能够重定向到我们的服务器以及某人的服务器,并且不需要知道它的来源或去向,只需保留相同的POST,GET和HEADER信息以及在此步骤中收到的其他信息.

More information: I am writing a routing page, which adds some custom information to query string and then routes all of the data, old and new further to some URL that was provided. This page should be able to redirect to our server as well as to someone's server and it doesn't need to know where it came from or where it goes, it just simply needs to keep the same POST, GET and HEADER information as well as additional information received at this step.

推荐答案

实际上,我可以通过混合Javascript和Codebehind代码来达到所需的结果.因此,我要做的是在服务器端代码中,我建立了一个完整的网页,如下所示:

Actually I was able to achieve the desired result by mixing Javascript and Codebehind code. So, what I've done is in server side code I've built an entire web page like following:

    var strForm = new StringBuilder();
    strForm.Append("<form id=\"" + formId + "\" name=\"" + formId + "\" action=\"" + url + queryString +"\" method=\"POST\">");
    foreach (string key in data)
    {
        strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key].Replace("\"", "&quot;") + "\">");
    }
    strForm.Append("</form>");

除了在服务器端构建此表单外,我还添加了一个JavaScript代码,用于提交我刚刚构建的表单.

And in addition to this form built on server side, I add a javascript code that submits the form I've just built.

    var strScript = new StringBuilder();
    strScript.Append("<script language='javascript'>");
    strScript.Append("var v" + formId + " = document." + formId + ";");
    strScript.Append("v" + formId + ".submit();");
    strScript.Append("</script>");
    strForm.Append("</form>");

因此,正如您所看到的,这段代码的作用是,表单操作是一个附加了查询字符串参数的URL ...但是由于form方法是POST,因此我们将添加为隐藏字段的值提交为POST参数...因此,我们最终同时提交了POST和GET参数.

So, what this code does, is as you see, the form action is an URL with query string parameters attached to it... but since the form method is POST, we submit the values we added as a hidden fields as POST parameters... So we end up submitting both POST and GET parameters.

希望此解决方案对某人有帮助=)

Hope this solution will help somebody =)

这篇关于带有POST和GET的HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆