2013-02-26 140 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