2012-04-19 69 views
0

在遠程網站上有一個頁面,用戶可以在表單中輸入輸入值,單擊按鈕並以輸出形式得到結果。 我想發送一個請求到頁面,填寫必要的輸入表單,提交併獲得一個結果頁面,輸出表格填寫。如何在csharp上提交後獲得響應頁面?

主題類似的所有線程給出代碼樣本是這樣的:

// Create a request using a URL that can receive a post. 
      WebRequest request = WebRequest.Create("http://www.site.com"); 
      // Set the Method property of the request to POST. 
      request.Method = "POST"; 
      // Create POST data and convert it to a byte array. 
      string postData = string.Format("inputParam=value"); 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      // Set the ContentType property of the WebRequest. 
      request.ContentType = "application/x-www-form-urlencoded"; 
      // Set the ContentLength property of the WebRequest. 
      request.ContentLength = byteArray.Length; 
      // Get the request stream. 
      Stream dataStream = request.GetRequestStream(); 
      // Write the data to the request stream. 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      // Close the Stream object. 
      dataStream.Close(); 
      // Get the response. 
      WebResponse response = request.GetResponse(); 
      // Get the stream containing content returned by the server. 
      dataStream = response.GetResponseStream(); 

      response.Close(); 

它返回一個頁面,填寫輸入形式,但輸出領域仍是空白,就像它不會做一個提交按鈕的點擊。

如何從我的C#代碼執行提交併接收帶有輸出數據的html文檔?

回答

0

將webresponse部分更改爲以下內容;

WebResponse response = request.GetResponse();     
    dataStream = response.GetResponseStream(); 
    StreamReader reader = new StreamReader (dataStream); 
    string responseFromServer = reader.ReadToEnd(); 
+0

好的,那麼我只是從流中的字符串。但問題是響應不包含輸出數據。 – Sindoki 2012-04-19 07:21:00

+0

預期響應是什麼? – daryal 2012-04-19 07:23:34

+0

有一個網站網址:「http://babelfish.yahoo.com/translate_txt」。示例參數是:「lp = en_it&trtext = sea」。在響應頁面中,name =「p」的元素必須具有值=「mare」,但它是空白的。 – Sindoki 2012-04-19 07:39:37

相關問題