2018年3月20日 星期二

[研究][ASP.NET] 讀網頁內容到字串

[研究][ASP.NET] 讀網頁內容到字串

2018-03-20

WebClient Constructor (System.Net) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.net.webclient.-ctor?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-TW%26k%3Dk(System.Net.WebClient.%2523ctor);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.2);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7.1

   try {

// Download the data to a buffer.
      WebClient client = new WebClient();

Byte[] pageData = client.DownloadData("http://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);

// Download the data to a file.
        client.DownloadFile("http://www.contoso.com", "page.htm");

// Upload some form post values.
NameValueCollection form = new NameValueCollection();
form.Add("MyName", "MyValue");
Byte[] responseData = client.UploadValues("http://www.contoso.com/form.aspx", form);

      }
      catch (WebException webEx) {
        Console.WriteLine(webEx.ToString());
          if(webEx.Status == WebExceptionStatus.ConnectFailure) {
          Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
          }
      }


string html = new WebClient().DownloadString("https://www.hinet.net/");



string html = await new WebClient().DownloadStringTaskAsync("https://www.hinet.net/");



using (WebClient wc = new WebClient())
{
    string html = wc.DownloadString(address);
}


WebRequest Class (System.Net) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.net.webrequest?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-TW%26k%3Dk(System.Net.WebRequest);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.2);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7.1


            // Create a request for the URL.
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();



var req = WebRequest.Create("https://www.hinet.net/");
req.BeginGetResponse(r =>
{
    var response = req.EndGetResponse(r);
    var stream = response.GetResponseStream();
    var reader = new StreamReader(stream, true);
    var str = reader.ReadToEnd();
    Console.WriteLine(str);
}, null);




(待續)

相關

HttpWebRequest Class (System.Net) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.net.httpwebrequest?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-TW%26k%3Dk(System.Net.HttpWebRequest);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.2);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7.1




沒有留言:

張貼留言