0

I've written a code that set a downloaded HTML-document into the variable. But i don't need all the HTML-document, only first 200 bytes of it. I need to cancel the method 'System.Net.WebClient.DownloadString' when it saves the document enough.

try
{
    WebClient webClient = new WebClient();
    html = webClient.DownloadString("https://example.com/index.html");
} catch(Exception e) {
     MessageBox.Show(e.Message);
}
Kick121
  • 3
  • 1
  • what is the desired result and what are you getting in `html`, because `DownloadString` returns a string. Not a byte array. – Ryan Wilson Aug 05 '19 at 17:17
  • I would like to the function doesn't download all the string of HTML-document because it spends the internet truffic and loads the central processor more then i need. I want to limit it. – Kick121 Aug 05 '19 at 17:35
  • With modern computing hardware and networking, is this really needed? Unless the document is huge, you could be over-optimizing and adding unnecessary complexity to your application. Just a thought! – Doug Dawson Aug 05 '19 at 18:28

3 Answers3

0

You can't read first N chars using WebClient since it reads response till the end.

Assuming that for some reason you can't use HttpClient, use WebReposense and GetResponseStream in particular to read a part of response.

Note, that "first N bytes" != "first N chars". You need to try to convert bytes to string using appropriate encoding, and use string only if conversion was successful.

Dennis
  • 37,026
  • 10
  • 82
  • 150
0

Try the sample given below.It uses the more modern HttpClient instead of a WebClient. I'm not sure if it actually limits the amount of bytes to 200 (see also How to retrieve partial response with System.Net.HttpClient) but you can give it a try.

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main()
        {
            var client = new HttpClient();
            using (var response = await client.GetAsync("https://example.com/index.html"))
            using (var stream = await response.Content.ReadAsStreamAsync())
            {
                var buffer = new byte[200];
                var count = await stream.ReadAsync(buffer, 0, buffer.Length);
                var result = Encoding.UTF8.GetString(buffer);
            }
        }
    }
}
Ruchi
  • 1,238
  • 11
  • 32
vdL
  • 263
  • 1
  • 8
0

As an option:

public async Task<string> GetPartialResponseAsync(string url, int length)
{
    var request = System.Net.WebRequest.Create(url);
    request.Method = "GET";

    using (var response = await request.GetResponseAsync())
    using (var responseStream = response.GetResponseStream())
    {
        byte[] buffer = new byte[length];
        await responseStream.ReadAsync(buffer, 0, length);

        return System.Text.Encoding.Default.GetString(buffer);
    }
}
Vitalii Ilchenko
  • 578
  • 1
  • 3
  • 7