public void GetResponse() { try { wc.UserAgent = UserAgent; wc.Method = Method; wc.KeepAlive = KeepAlive; wc.Timeout = TimeOut; wc.Accept = Accept; wc.Credentials = CredCache; using (var response = (HttpWebResponse)wc.GetResponse()) { ContentLength = response.ContentLength; ContentType = response.ContentType.Split(';')[0]; StatusCode = response.StatusCode; using (var strm = new BufferedStream(response.GetResponseStream())) { using (var str = new StreamReader(strm, true)) { using (var evArgs = new HTTPResponseEventArgs(str.ReadToEnd())) HTTPClientResponse?.Invoke(this, evArgs); str.Close(); } strm.Close(); } response.Close(); } } catch (WebException ex) { HTTPClientError?.Invoke(this, new HTTPErrorEventArgs((int)ex.Status, ex.Message)); } }
public void GetResponse(string name, bool etag = false, bool icyClient = false) { try { wc.UserAgent = useragent; wc.Method = method; if (etag) { wc.Headers.Add("ETag", MD5.GetMD5Hash(this.url)); wc.Headers.Add("Icy-Metadata", "1"); } using (var response = (HttpWebResponse)wc.GetResponse()) { contentLength = response.ContentLength; contentType = response.ContentType.Split(';')[0]; statuscode = response.StatusCode; using (var str = new StreamReader(new BufferedStream(response.GetResponseStream()), true)) { var result = str.ReadToEnd(); Task.Run(() => HTTPClientResponse?.Invoke(this, new HTTPClientResponseEventArgs(result))); str.BaseStream.Close(); str.Close(); } } } catch (Exception ex) { Task.Run(() => { HTTPClientError?.Invoke(this, new HTTPClientErrorEventArgs( string.Format("HTTPClient Error: ({0}) {1}", name, ex.Message))); }); } }