2013-02-26 101 views
1

我编程方式创建这样获取查询字符串变量webrequested

 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(); 

一个Web请求,并在printpdf.aspx页面(你可以看到它的网址)我想要得到的查询字符串参数,当这个URL以编程方式执行时。当我尝试平常的方式

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

它不起作用。 有什么我在做错误的方式。还是有没有更好的方法来做到这一点?

+0

你在做这个两个不同的过程,对吗? (例如:在IIS中托管的独立控制台应用程序和ASP.NET Web应用程序)是否在请求在“控制台应用程序”中发送之前检查了请求的查询参数是否可访问?还有,您是否检查过HttpContext.Current.Request.Url(在“网站”)是你真正想要发送? – 2013-02-26 10:48:54

回答

1

究竟在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); 
    } 
+0

我试过this.at Debug.WriteLine(“关于发送请求与查询= \”{0} \“” ,request.RequestUri.Query); 我得到正确的values.and在服务器端我没有使用aspx页面,所以我真的可以使用你建议的方法,所以我试过这个 HttpContext.Current.Request.Url.ToString( ) 并且它返回一个没有任何查询字符串参数的URL 只有这么多 http:// aksphases:201/min-konto/printpdf.aspx – Athul 2013-02-26 11:21:45

+0

你在服务器上使用什么?它是一个通用的哈ndler?你在做ASP.NET MVC吗? – 2013-02-26 11:27:55

+0

它是一个Umbraco(建立在MVC上的asp.net cms)website.and我只能使用简单的asp.net类文件做事 – Athul 2013-02-26 11:42:43