ฉันจะดาวน์โหลดซอร์ส HTML ใน C # ได้อย่างไร


109

ฉันจะรับซอร์ส HTML ที่ระบุที่อยู่เว็บใน c # ได้อย่างไร

คำตอบ:


185

คุณสามารถดาวน์โหลดไฟล์ด้วยคลาส WebClient :

using System.Net;

using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{
    client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");

    // Or you can get the file content without saving it
    string htmlCode = client.DownloadString("http://yoursite.com/page.html");
}

ข้อควรทราบ: หากต้องการการควบคุมเพิ่มเติมให้ดูที่คลาส HttpWebRequest (เช่นสามารถระบุการพิสูจน์ตัวตน)
Richard

1
ใช่ HttpWebRequest ช่วยให้คุณสามารถควบคุมได้มากขึ้นแม้ว่าคุณจะสามารถส่งคำขอ POST กับ WebClient ได้โดยใช้ client.UploadData (uriString, "POST", postParamsByteArray);
CMS

1
จะไม่ระมัดระวังที่จะจับ WebException เกี่ยวกับเรื่องนี้หรือไม่? บางทีอาจจะถูกสันนิษฐาน ต้องมีข้อยกเว้นหรือข้อผิดพลาดอื่น ๆ ด้วยวิธีนี้หรือไม่?
John Washam

4
@JohnWasham - ใช่มันจะเป็นการดีที่จะจับข้อยกเว้นที่นี่ อย่างไรก็ตามโชคดีที่ผู้ตอบ StackOverflow ส่วนใหญ่เก็บโค้ดตัวอย่างให้ชัดเจนและรัดกุมที่สุด การทำให้โค้ดตัวอย่างใกล้เคียงกับ "ชีวิตจริง" มากขึ้นเพียงแค่เพิ่มเสียงรบกวน
Chris Rogers

ปัญหาที่ฉันเผชิญคือเมื่อฉันดาวน์โหลดเพจซอร์สและรับข้อมูลมากกว่าหากเว็บไซต์นั้นเป็นภาษาอื่นที่ไม่ใช่เพจซอร์ซของฉันจะไม่ได้รับค่าเหล่านั้น
Rush.2707

40

โดยพื้นฐาน:

using System.Net;
using System.Net.Http;  // in LINQPad, also add a reference to System.Net.Http.dll

WebRequest req = HttpWebRequest.Create("http://google.com");
req.Method = "GET";

string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
    source = reader.ReadToEnd();
}

Console.WriteLine(source);

20

คำตอบใหม่ล่าสุดล่าสุดและเป็นปัจจุบัน
โพสต์นี้เก่ามาก (เมื่อฉันตอบไป 7 ปี) ดังนั้นคำตอบอื่น ๆ จึงไม่มีคำตอบที่ใช้วิธีใหม่และที่แนะนำซึ่งก็คือHttpClientคลาส


HttpClientถือเป็น API ใหม่และควรแทนที่อันเก่า ( WebClientและWebRequest)

string url = "page url";
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
   using (HttpContent content = response.Content)
   {
      string result = content.ReadAsStringAsync().Result;
   }
}

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีใช้HttpClientคลาส (โดยเฉพาะในกรณี async) คุณสามารถอ้างอิงคำถามนี้ได้


หมายเหตุ 1: หากคุณต้องการใช้ async / await

string url = "page url";
HttpClient client = new HttpClient();
using (HttpResponseMessage response = await client.GetAsync(url))
{
   using (HttpContent content = response.Content)
   {
      string result = await content.ReadAsStringAsync();
   }
}

หมายเหตุ 2: หากใช้คุณสมบัติ C # 8

string url = "page url";
HttpClient client = new HttpClient();
using HttpResponseMessage response = await client.GetAsync(url);
using HttpContent content = response.Content;
string result = await content.ReadAsStringAsync();


4
คำแนะนำ: รอวิธี async
Maarten

@Maarten ลิงค์ต่อไปนี้แสดงวิธีใช้กับ async / await stackoverflow.com/questions/33020657/…
Hakan Fıstık

17

คุณสามารถรับได้ด้วย:

var html = new System.Net.WebClient().DownloadString(siteUrl)

สั้นและหวาน! ฉันพบข้อเสนอแนะของคุณหลังจากอ่านตัวอย่างของ Joe Albahari LINQPad> Help> มีอะไรใหม่และค้นหา Cache
Colin

7
var html = System.Net.WebClient ใหม่ (). DownloadString (siteUrl); // ต้องสร้างลูกค้าใหม่!
user1328350

9
ไม่ว่า? DisposeWebClient
JD

11

วิธี @cms เป็นวิธีล่าสุดที่แนะนำในเว็บไซต์ MS แต่ฉันมีปัญหาอย่างหนักในการแก้ไขด้วยทั้งสองวิธีที่โพสต์ที่นี่ตอนนี้ฉันโพสต์วิธีแก้ปัญหาสำหรับทุกคน!

ปัญหา: หากคุณใช้ url เช่นนี้www.somesite.it/?p=1500ในบางกรณีคุณได้รับข้อผิดพลาดภายในเซิร์ฟเวอร์ (500) แม้ว่าในเว็บเบราว์เซอร์จะwww.somesite.it/?p=1500ทำงานได้ดี

วิธีแก้ปัญหา: คุณต้องย้ายพารามิเตอร์ออกรหัสการทำงานคือ:

using System.Net;
//...
using (WebClient client = new WebClient ()) 
{
    client.QueryString.Add("p", "1500"); //add parameters
    string htmlCode = client.DownloadString("www.somesite.it");
    //...
}

ที่นี่เอกสารอย่างเป็นทางการ

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.