जवाबों:
आप 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");
}
मूल रूप से:
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);
नवीनतम, सबसे हाल का, आज तक का उत्तर
यह पोस्ट वास्तव में पुराना है (यह 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 मामलों में), आप इस प्रश्न का उल्लेख कर सकते हैं
आप इसे प्राप्त कर सकते हैं:
var html = new System.Net.WebClient().DownloadString(siteUrl)
DisposeWebClient
@ सेमी रास्ता अधिक हाल ही में, एमएस वेबसाइट में सुझाया गया है, लेकिन मुझे हल करने के लिए एक कठिन समस्या थी, दोनों विधि यहां पोस्ट की गई थी, अब मैं सभी के लिए समाधान पोस्ट करता हूं!
समस्या:
यदि आप इस तरह एक यूआरएल का उपयोग करते हैं: 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");
//...
}