1

我正在以编程方式创建这样的网络请求

        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"]

它不起作用。有什么我做错了。或者有没有更好的方法来做到这一点?

4

1 回答 1

1

您在 Web App 的哪个位置调用它?

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);
    }
于 2013-02-26T10:58:30.703 回答