protected WebResponse executeRequest(HttpWebRequest request) { // request configration string[] acceptEncodingValues = request.Headers.GetValues("Accept-Encoding"); if (acceptEncodingValues == null || acceptEncodingValues.Length == 0) { // we support gzip if nothing else was specified. request.Headers.Add("Accept-Encoding", "gzip"); } if (request.UserAgent == null) { request.UserAgent = lyricsReloaded.getDefaultUserAgent(); } request.Accept = "*/*"; request.Headers.Add("Accept-Encoding", "gzip"); // load the response WebResponse webResponse; String contentString = null; Encoding encoding = Encoding.ASCII; // default encoding as the last fallback HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout) { throw new WebException("The operation has timed out.", e); } else if (e.Status == WebExceptionStatus.ProtocolError || e.Status == WebExceptionStatus.UnknownError) { throw new WebException("Ops, something went wrong. (Report this please)", e); } throw; } using (response) { if (response.CharacterSet != null) { encoding = Encoding.GetEncoding(response.CharacterSet); // the response encoding specified by the server. this should be enough } Stream responsesStream = response.GetResponseStream(); if (responsesStream != null) { responsesStream.ReadTimeout = timeout; if (String.Compare(response.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase) == 0) { // gzip compression detected, wrap the stream with a decompressing gzip stream lyricsReloaded.getLogger().debug("gzip compression detected"); responsesStream = new GZipStream(responsesStream, CompressionMode.Decompress); } MemoryStream content = new MemoryStream(); const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int bytesRead; do { bytesRead = responsesStream.Read(buffer, 0, bufferSize); if (bytesRead <= 0) { break; } content.Write(buffer, 0, bytesRead); }while (bytesRead > 0); responsesStream.Close(); contentString = encoding.GetString(content.GetBuffer()); // decode the data with the currently known encoding Match match = ENCODING_REGEX.Match(contentString); // search for a encoding specified in the content if (match.Success) { try { Encoding tmp = Encoding.GetEncoding(match.Groups[1].ToString()); // try to get a encoding from the name if (!encoding.Equals(tmp)) { encoding = tmp; contentString = encoding.GetString(content.GetBuffer()); // decode again with the newly found encoding } } catch (ArgumentException) {} } content.Close(); } webResponse = new WebResponse(contentString, encoding, response.Headers); } return(webResponse); }