2010-05-16 30 views
0

.NET中最好和最簡單的http用戶代理是什麼?.NET中最好和最簡單的http用戶代理是什麼?

我只是想把它放在網址中,讓它將頁面作爲字符串返回。

+2

你能否澄清你的意思是「用戶代理」?你只想要一個處理URL請求的庫嗎?或者你想要一些輕量級的應用程序,你可以發送HTTP請求並以原始格式查看它們? – 2010-05-16 05:08:39

+0

'用戶代理'的定義:en.wikipedia.org/wiki/User_agent – CJ7 2010-05-16 05:53:34

回答

2

感謝@ion todriel,基於System.Net.HttpWebRequest一個建議:

using System; 
using System.Collections.Generic; 
using System.Net; 
using System.IO; 

namespace myHttpWebRequest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var request = HttpWebRequest.Create("http://www.example.com"); 
      var response = request.GetResponse(); 
      var reader = new StreamReader(response.GetResponseStream()); 
      string page = reader.ReadToEnd(); 
      Console.Write(page); 
     } 
    } 
} 

注行string page = reader.ReadToEnd(); - 返回整個頁面作爲一個字符串。

這不比具有參考文獻中的例子的較早的 System.Net.WebClinet 複雜。

+0

C-e C-b C-b C-t – 2010-05-16 05:11:32

+0

簡單但註定失敗。 WebClient寫得很差,它從Component繼承,需要處理,而且在服務器響應有時超過1秒(WebClient沒有設置超時間隔和默認時間間隔非常短)的真實場景中,這是不切實際的。幫你一個使用WebRequest。 – 2010-05-16 05:45:05

+0

謝謝 - @Ion Todriel。修改後顯示HttpWebRquest。 – gimel 2010-05-16 06:27:50

相關問題