从程序创建的Web请求中获取查询字符串变量

本文关键字:获取 查询 字符串 变量 请求 程序 创建 Web | 更新日期: 2023-09-27 18:01:15

我正在以编程方式创建一个类似的web请求

        string url = "http://aksphases:201/min-konto/printpdf.aspx?id=149656222&name=Ink%20And%20Toner";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = new CookieContainer(); // required for HttpWebResponse.Cookies
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        byte[] data = Encoding.UTF8.GetBytes("email=mymail&password=1234");
        request.ContentLength = data.Length;
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        HttpWebResponse myWebResponse = (HttpWebResponse)request.GetResponse();
        Stream ReceiveStream = myWebResponse.GetResponseStream();

printpdf.aspx页面中(你可以在url中看到它(,当这个url被程序化执行时,我想得到查询字符串参数。当我尝试通常的方式

HttpContext.Current.Request.QueryString["id"]

它不起作用。我有没有做错什么。或者有更好的方法吗?

从程序创建的Web请求中获取查询字符串变量

您在Web应用程序中究竟在哪里调用此函数?

HttpContext.Current.Request.QueryString["id"]

以下是我认为你应该尝试的:在您的客户端应用程序中:

    string url = "http://aksphases:201/min-konto/printpdf.aspx?id=149656222&name=Ink%20And%20Toner";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    // try this
    Debug.WriteLine("About to send request with query='"{0}'"", request.RequestUri.Query);
    // and check to see what gets printed in the debug output windows
    request.CookieContainer = new CookieContainer(); // required for HttpWebResponse.Cookies
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    byte[] data = Encoding.UTF8.GetBytes("email=mymail&password=1234");
    request.ContentLength = data.Length;

而在你的ASPX页面上,试试这个:

    protected void Page_Load(object sender, EventArgs e) {
        var theUrl = this.Request.Url.ToString();
        Debug.WriteLine(theUrl); // is this the exact URL that you initially requested ?
        // if you have FormsAuthentication or other redirects
        // this might get modified if you're not careful
        var theId = this.Request.QueryString["id"];
        Debug.WriteLine(theId);
    }