WebページをダウンロードするC#。より良い方法が必要、CPU使用率が高い

StackOverflow https://stackoverflow.com/questions/225617

  •  03-07-2019
  •  | 
  •  

質問

このコードを少しうまく機能させようとしています。私はそれが一度に1バイトを読み取るループだと思う。 gzip解凍でこれを行う別の方法を見つけることができませんでした。 StreamReader の実装は問題ありませんが、圧縮解除ストリームに渡すことができない文字列を返します。

もっと良い方法はありますか?

byte[] bufffer = null;
List<byte> resourceBytes = new List<byte>();
int byteValue = 0;
WebResource resource = new WebResource();
HttpWebResponse webResponse = null;

try {
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(resourceUri);
    webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    webRequest.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    webRequest.UserAgent = agent;
    webRequest.Accept = "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1";
    webRequest.Credentials = CredentialCache.DefaultCredentials;
    webRequest.Referer = resourceUri.OriginalString;
    webRequest.Timeout = 5000;

    webResponse = (HttpWebResponse)webRequest.GetResponse();

    Stream webStream = webResponse.GetResponseStream();

    if (!string.IsNullOrEmpty(webResponse.ContentEncoding)) {
        if (webResponse.ContentEncoding.ToLower().Contains("gzip")) {
            webStream = new GZipStream(webStream, CompressionMode.Decompress);
        }
        else if (webResponse.ContentEncoding.ToLower().Contains("deflate")) {
            webStream = new DeflateStream(webStream, CompressionMode.Decompress);
        }
    }

    do {
        byteValue = webStream.ReadByte();

        if (byteValue != -1) {
            resourceBytes.Add((byte)byteValue);
        }

    } while (byteValue != -1);


    //Free up resources
    webStream.Close();
    webResponse.Close();

    bufffer = resourceBytes.ToArray();
役に立ちましたか?

解決

WebClient、特にWebClient.DownloadDataの方がはるかにシンプルになるというjmcdに同意します。

実際の質問です。問題は、おそらく固定バッファとループが必要なときに、1バイトを読んでいるということです。つまり、

int bytesRead;
byte[] buffer = new byte[1024];
while((bytesRead = webStream.Read(buffer, 0, buffer.Length)) > 0) {
  // process "bytesRead" worth of data from "buffer"
}

[強調を編集]重要な点は、&quot; bytesRead&quot;を処理するのはのみ毎回のデータの価値。そこにあるものはすべてゴミです。

scroll top