2012-06-04 30 views
0

請參考下面的代碼。我如何測試/調試,下面的代碼是否正確工作。它運行並且沒有編譯/運行時錯誤。找出httpwebrequest的結果POST

以下代碼的最終結果是設置頁面中的一個控件,以從下面的代碼保存POST數據。然而,我還沒有到那麼遠。

protected override void OnInit(EventArgs e) 
{ 

    ASCIIEncoding encoding = new ASCIIEncoding(); 
    string postData = "http://s.com/is/image/scom/2Peel"; 
    byte[] data = encoding.GetBytes(postData); 

    // Prepare web request... 
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://m.com/Confirm.aspx?ID=175"); 
    myRequest.Method = "POST"; 
    myRequest.ContentType = "application/x-www-form-urlencoded"; 
    myRequest.ContentLength = data.Length; 
    Stream newStream = myRequest.GetRequestStream(); 
    // Send the data. 
    newStream.Write(data, 0, data.Length); 
    newStream.Close(); 

} 

更新1:我試圖找出使用下面的代碼的響應,但頁面沒有加載。

HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse(); 

Console.WriteLine("Content length is {0}", response.ContentLength); 
Console.WriteLine("Content type is {0}", response.ContentType); 

回答

0

用你的HttpWebRequest對象的GetResponse方法(在名爲 「myRequest」)。

+0

HttpWebResponse response =(HttpWebResponse)myRequest.GetResponse(); Console.WriteLine(「Content length is {0}」,response.ContentLength); Console.WriteLine(「Content type is {0}」,response.ContentType);我試圖找出上面的代碼,但即使在我等了10分鐘後,頁面也沒有加載。 – NewCoder

+0

這很奇怪...你想要訪問哪個頁面?你可以在瀏覽器中訪問它嗎? – ekolis