public void GetAsFile(string filename, FileMode fileMode, FileAccess fileAccess) { var response = GetResponse(); try { using (var fileStream = new FileStream(filename, fileMode, fileAccess)) { using (Stream responseStream = response.GetResponseStream()) { var stream = WebClientService.GetTimeoutStream(responseStream, Timeout); CopyStream(stream, fileStream); } } } catch (AsyncTimeoutException e) { throw new WebClientException("Master timeout exception", e); } catch (Exception e) { throw new WebClientException("Unable to read response from server", e); } }
public byte[] GetAsByteArray() { var response = GetResponse(); try { using (var fileStream = new MemoryStream()) { using (Stream responseStream = response.GetResponseStream()) { var stream = WebClientService.GetTimeoutStream(responseStream, Timeout); CopyStream(stream, fileStream); } return(fileStream.ToArray()); } } catch (AsyncTimeoutException e) { throw new WebClientException("Master timeout exception", e); } catch (Exception e) { throw new WebClientException("Unable to read response from server", e); } }
private HttpWebResponse GetResponse() { if (Response == null) { HttpWebResponse webResponse = null; try { EnsureParametersSetBeforeCall(); byte[] data = PostData; if (data != null) { Stream requestStream = null; Request.ContentLength = data.Length; try { requestStream = WebClientService.GetRequestStreamWithTimeout(Request, Timeout); } catch (Exception e) { Request.Abort(); throw new WebClientException("Unable to get response from server", e); } try { WebClientService.ExecSyncWithTimeout ( (callback, state) => requestStream.BeginWrite(data, 0, data.Length, callback, state), requestStream.EndWrite, Timeout ); } catch (Exception e) { Request.Abort(); throw new WebClientException("Unable to send data to server", e); } requestStream.Close(); } webResponse = WebClientService.GetResponseWithTimeout(Request, Timeout); } catch (Exception e) { Request.Abort(); throw new WebClientException("Unable to get response from server", e); } if (!IsAcceptedStatusCode(webResponse.StatusCode)) { throw new WebClientException(string.Format("The server did not respond with accepted status code, but with [{0}]", webResponse.StatusCode)); } Response = webResponse; } return(Response); }
private void EnsureParametersSetBeforeCall() { Request = (HttpWebRequest)WebRequest.Create(GetCompleteUrl()); if (string.IsNullOrEmpty(Method)) { Request.Method = "GET"; } else { Request.Method = Method; } Request.CookieContainer = CookieContainer ?? WebClientService.CookieContainer; foreach (var clientCertificate in ClientCertificates) { Request.ClientCertificates.Add(clientCertificate); } Request.UserAgent = UserAgent ?? (string.IsNullOrEmpty(UserAgentVersion) ? WebClientService.GetUserAgent(null) : WebClientService.GetUserAgent(UserAgentVersion)); Request.KeepAlive = KeepAlive; Request.ContentType = ContentType; if (HasSupportGZip) { Request.AutomaticDecompression = DecompressionMethods.GZip; Request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip"); } foreach (var headerByHttpRequestHeader in HeadersByHttpRequestHeader) { Request.Headers.Add(headerByHttpRequestHeader.Key, headerByHttpRequestHeader.Value); } foreach (var headerByString in HeadersByString) { Request.Headers.Add(headerByString.Key, headerByString.Value); } foreach (var header in Headers) { Request.Headers.Add(header); } }
public WebClientRequest(WebClientService webClientService, string url) { WebClientService = webClientService; Url = url; }