protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { Stream compressStream = null; switch (_encodingType) { case "gzip": compressStream = new GZipStream(stream, CompressionMode.Compress, true); break; case "deflate": compressStream = new DeflateStream(stream, CompressionMode.Compress, true); break; default: compressStream = stream; break; } return(_originalContent.CopyToAsync(compressStream).ContinueWith(tsk => { if (compressStream != null) { compressStream.Dispose(); } })); }
/// <summary> /// Serialize the HTTP content to a stream as an asynchronous operation. /// </summary> /// <param name="stream">The target stream.</param> /// <param name="context">Information about the transport (channel binding token, for example). This parameter may be null.</param> /// <returns> /// Returns <see cref="T:System.Threading.Tasks.Task" />.The task object representing the asynchronous operation. /// </returns> protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { Stream compressedStream = null; if (_originalContent == null) { return(Task.FromResult(( object )null)); } if (_encodingType == "gzip") { compressedStream = new GZipStream(stream, CompressionMode.Compress, true); } else if (_encodingType == "deflate") { compressedStream = new DeflateStream(stream, CompressionMode.Compress, true); } return(_originalContent.CopyToAsync(compressedStream).ContinueWith(tsk => { if (compressedStream != null) { compressedStream.Dispose( ); } })); }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (var zip = new DeflateStream(stream, CompressionMode.Compress, true)) { await content.CopyToAsync(zip); } }
protected override async Task SerializeToStreamAsync(System.IO.Stream stream, TransportContext context) { using (var gzip = new GZipStream(stream, CompressionMode.Compress, true)) { await _content.CopyToAsync(gzip); } }
/// <summary> /// Downloads the specified URL into the specified file. /// </summary> /// <param name="url">The URL to download</param> /// <param name="token">The authentication token to use</param> /// <param name="outputFilename">Where to write the file</param> static public async Task HttpDownloadAsync(string url, string token, string outputFilename) { Log.Info(LogTag, "Performing HttpDownloadAsync, URL {0}", url); using (HttpClient client = new HttpClient(new NativeMessageHandler())) { var request = new HttpRequestMessage() { RequestUri = new System.Uri(url), Method = HttpMethod.Get }; if (token != null) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); } using (HttpResponseMessage response = await client.SendAsync(request)) using (HttpContent responseContent = response.Content) { // Copy the results out to the specified file. using (FileStream output = File.OpenWrite(outputFilename)) { await responseContent.CopyToAsync(output); } } } }
public static async Task <T> ResponseContentToModel <T>(HttpContent hc, XmlSerializer serializer = null) where T : new() { if (serializer == null) { serializer = new XmlSerializer(typeof(T)); } using (var stream = new MemoryStream()) { var contentString = await hc.ReadAsStringAsync(); // response is a collection and it is empty: if (contentString.Contains("nil-classes")) { return(new T()); } await hc.CopyToAsync(stream); stream.Position = 0; // set stream to beginning, or deserialization will bomb. var model = (T)serializer.Deserialize(stream); return(model); } }
private StreamContent ConvertToStreamContent(HttpContent originalContent) { if (originalContent == null) { return(null); } StreamContent streamContent = originalContent as StreamContent; if (streamContent != null) { return(streamContent); } MemoryStream ms = new MemoryStream(); // **** NOTE: ideally you should NOT be doing calling Wait() as its going to block this thread **** // if the original content is an ObjectContent, then this particular CopyToAsync() call would cause the MediaTypeFormatters to // take part in Serialization of the ObjectContent and the result of this serialization is stored in the provided target memory stream. originalContent.CopyToAsync(ms).Wait(); // Reset the stream position back to 0 as in the previous CopyToAsync() call, // a formatter for example, could have made the position to be at the end after serialization ms.Position = 0; streamContent = new StreamContent(ms); // copy headers from the original content foreach (KeyValuePair <string, IEnumerable <string> > header in originalContent.Headers) { streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(streamContent); }
/// <summary> /// Reads the content of the HTTP response as string asynchronously. /// </summary> /// <param name="content">The content.</param> /// <param name="encoding">The encoding. You can leave this parameter null and the encoding will be /// automatically calculated based on the charset in the response. Also, UTF-8 /// encoding will be used if the charset is absent from the response, is blank /// or contains an invalid value.</param> /// <returns>The string content of the response.</returns> /// <remarks> /// This method is an improvement over the built-in ReadAsStringAsync method /// because it can handle invalid charset returned in the response. For example /// you may be sending a request to an API that returns a blank charset or a /// mispelled one like 'utf8' instead of the correctly spelled 'utf-8'. The /// built-in method throws an exception if an invalid charset is specified /// while this method uses the UTF-8 encoding in that situation. /// /// My motivation for writing this extension method was to work around a situation /// where the 3rd party API I was sending requests to would sometimes return 'utf8' /// as the charset and an exception would be thrown when I called the ReadAsStringAsync /// method to get the content of the response into a string because the .Net HttpClient /// would attempt to determine the proper encoding to use but it would fail due to /// the fact that the charset was misspelled. I contacted the vendor, asking them /// to either omit the charset or fix the misspelling but they didn't feel the need /// to fix this issue because: /// "in some programming languages, you can use the syntax utf8 instead of utf-8". /// In other words, they are happy to continue using the misspelled value which is /// supported by "some" programming languages instead of using the properly spelled /// value which is supported by all programming languages. /// </remarks> /// <example> /// <code> /// var httpRequest = new HttpRequestMessage /// { /// Method = HttpMethod.Get, /// RequestUri = new Uri("https://api.vendor.com/v1/endpoint") /// }; /// var httpClient = new HttpClient(); /// var response = await httpClient.SendAsync(httpRequest, CancellationToken.None).ConfigureAwait(false); /// var responseContent = await response.Content.ReadAsStringAsync(null).ConfigureAwait(false); /// </code> /// </example> public static async Task <string> ReadAsStringAsync(this HttpContent content, Encoding encoding) { var responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false); var responseContent = string.Empty; if (encoding == null) { encoding = content.GetEncoding(Encoding.UTF8); } // This is important: we must make a copy of the response stream otherwise we would get an // exception on subsequent attempts to read the content of the stream using (var ms = new MemoryStream()) { await content.CopyToAsync(ms).ConfigureAwait(false); ms.Position = 0; using (var sr = new StreamReader(ms, encoding)) { responseContent = await sr.ReadToEndAsync().ConfigureAwait(false); } } return(responseContent); }
public static Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite) { string pathname = Path.GetFullPath(filename); if (!overwrite && File.Exists(filename)) { throw new InvalidOperationException(string.Format("File {0} already exists.", pathname)); } FileStream fileStream = null; try { fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None); return(content.CopyToAsync(fileStream).ContinueWith( (copyTask) => { fileStream.Close(); })); } catch { if (fileStream != null) { fileStream.Close(); } throw; } }
private async Task <HttpContent> CloneHttpContentAsync(HttpContent httpContent) { var temp = new MemoryStream(); await httpContent.CopyToAsync(temp).ConfigureAwait(false); temp.Position = 0; var clone = new StreamContent(temp); if (httpContent.Headers != null) { foreach (var h in httpContent.Headers) { clone.Headers.Add(h.Key, h.Value); } } #if WINDOWS_APP // WORKAROUND // On UWP there is a bug in the Http stack that causes an exception to be thrown when moving around a stream. // https://stackoverflow.com/questions/31774058/postasync-throwing-irandomaccessstream-error-when-targeting-windows-10-uwp // LoadIntoBufferAsync is necessary to buffer content for multiple reads - see https://stackoverflow.com/questions/26942514/multiple-calls-to-httpcontent-readasasync // Documentation is sparse, but it looks like loading the buffer into memory avoids the bug, without // replacing the System.Net.HttpClient with Windows.Web.Http.HttpClient, which is not exactly a drop in replacement await clone.LoadIntoBufferAsync().ConfigureAwait(false); #endif return(clone); }
internal static Task WriteStreamedResponseContentAsync(HttpResponseBase httpResponseBase, HttpContent responseContent) { Contract.Assert(httpResponseBase != null); Contract.Assert(responseContent != null); Task writeResponseContentTask = null; try { // Copy the HttpContent into the output stream asynchronously. writeResponseContentTask = responseContent.CopyToAsync(httpResponseBase.OutputStream); } catch { // Streamed content may have been written and cannot be recalled. // Our only choice is to abort the connection. AbortConnection(httpResponseBase); return(TaskHelpers.Completed()); } return(writeResponseContentTask .Catch((info) => { // Streamed content may have been written and cannot be recalled. // Our only choice is to abort the connection. AbortConnection(httpResponseBase); return info.Handled(); })); }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (var uncloseableStream = new UndisposableStream(stream)) #if !DNXCORE50 using (var bufferedStream = new BufferedStream(uncloseableStream)) #endif { #if !DNXCORE50 var streamToUse = bufferedStream; #else var streamToUse = uncloseableStream; #endif Stream compressedStream = null; if (encodingType == "gzip") { compressedStream = new GZipStream(streamToUse, CompressionMode.Compress, leaveOpen: true); } else if (encodingType == "deflate") { compressedStream = new DeflateStream(streamToUse, CompressionMode.Compress, leaveOpen: true); } else { throw new InvalidOperationException("This shouldn't happen, ever."); } await originalContent.CopyToAsync(compressedStream); if (compressedStream != null) { compressedStream.Dispose(); } } }
internal static async Task WriteBufferedResponseContentAsync(HttpContextBase httpContextBase, HttpContent responseContent, HttpRequestMessage request) { Contract.Assert(httpContextBase != null); Contract.Assert(responseContent != null); Contract.Assert(request != null); HttpResponseBase httpResponseBase = httpContextBase.Response; // Return a task that writes the response body asynchronously. // We guarantee we will handle all error responses internally // and always return a non-faulted task. Exception exception = null; try { // Copy the HttpContent into the output stream asynchronously. await responseContent.CopyToAsync(httpResponseBase.OutputStream); } catch (Exception e) { // Can't use await inside a catch block exception = e; } if (exception != null) { // If we were using a buffered stream, we can still set the headers and status // code, and we can create an error response with the exception. // We create a continuation task to write an error response that will run after // returning from this Catch() but before other continuations the caller appends to this task. // The error response writing task handles errors internally and will not show as faulted. await CreateErrorResponseAsync(httpContextBase, responseContent, request, exception); } }
public async Task WriteNewFile(HttpContent content) { using (var fileToWrite = new FileStream(this.GetPath(), FileMode.Create, FileAccess.Write)) { await content.CopyToAsync(fileToWrite); } }
public async Task HttpContentToFile(HttpContent inputstream, string filepath) { Directory.CreateDirectory(Path.GetDirectoryName(filepath)); using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None)) { await inputstream.CopyToAsync(stream); } }
public byte[] Get(string url, bool returnstream = true, bool redownload = false) { var uri = new Uri(url.ToLower()); string cleanname = uri.AbsolutePath; if (redownload || !File.Exists(cacheDir + cleanname)) { try { if (!Directory.Exists(cacheDir + cleanname)) { Directory.CreateDirectory(Path.GetDirectoryName(cacheDir + cleanname)); } Console.Write("\nDownloading " + cleanname); using (HttpResponseMessage response = client.GetAsync(uri).Result) { if (response.IsSuccessStatusCode) { using (MemoryStream mstream = new MemoryStream()) using (HttpContent res = response.Content) { res.CopyToAsync(mstream); if (isEncrypted) { var cleaned = Path.GetFileNameWithoutExtension(cleanname); var decrypted = BLTE.DecryptFile(cleaned, mstream.ToArray(), decryptionKeyName); File.WriteAllBytes(cacheDir + cleanname, decrypted); return(decrypted); } else { File.WriteAllBytes(cacheDir + cleanname, mstream.ToArray()); } } } else { throw new Exception("Error retrieving file: HTTP status code " + response.StatusCode + " on URL " + url); } } } catch (Exception e) { Console.WriteLine(e.Message); } } if (returnstream) { return(File.ReadAllBytes(cacheDir + cleanname)); } else { return(new byte[0]); } }
/// <inheritdoc/> protected sealed override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (var compressionStream = CreateCompressionStream(stream, context)) { await _inner.CopyToAsync(compressionStream, context).ConfigureAwait(false); await compressionStream.FlushAsync().ConfigureAwait(false); } }
static async Task <string> DownloadZip(HttpContent content) { var path = Path.GetTempFileName(); using (var output = File.Create(path)) await content.CopyToAsync(output).DontContinueOnCapturedContext(); return(path); }
/// <inheritdoc /> protected async override Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (content) using (var compressedStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true)) { await content.CopyToAsync(compressedStream); } }
static string DownloadZip(HttpContent content) { var path = Path.GetTempFileName(); using (var output = File.Create(path)) content.CopyToAsync(output).Wait(); return(path); }
protected async override Task SerializeToStreamAsync(Stream stream, TransportContext context) { if (_compressionMethod == CompressionMethod.GZip) { using (var gzipStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true)) { await _originalContent.CopyToAsync(gzipStream); } } else if (_compressionMethod == CompressionMethod.Deflate) { using (var deflateStream = new DeflateStream(stream, CompressionMode.Compress, leaveOpen: true)) { await _originalContent.CopyToAsync(deflateStream); } } }
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { // Reset stream to start position _content.Position = _start; // Copy result to output return(_byteRangeContent.CopyToAsync(stream)); }
/// <summary> /// Submits the http post request to the specified uri. /// </summary> /// <param name="uri">The uri to which the post request will be issued.</param> /// <param name="requestStream">Optional stream containing data for the request body.</param> /// <param name="requestStreamContentType">The type of that request body data.</param> /// <returns>Task tracking the completion of the POST request</returns> private async Task <Stream> PostAsync( Uri uri, Stream requestStream = null, string requestStreamContentType = null) { StreamContent requestContent = null; MemoryStream responseDataStream = null; if (requestStream != null) { requestContent = new StreamContent(requestStream); requestContent.Headers.Remove(ContentTypeHeaderName); requestContent.Headers.TryAddWithoutValidation(ContentTypeHeaderName, requestStreamContentType); } WebRequestHandler requestSettings = new WebRequestHandler(); requestSettings.UseDefaultCredentials = false; requestSettings.Credentials = this.deviceConnection.Credentials; requestSettings.ServerCertificateValidationCallback = this.ServerCertificateValidation; using (HttpClient client = new HttpClient(requestSettings)) { this.ApplyHttpHeaders(client, HttpMethods.Post); Task <HttpResponseMessage> postTask = client.PostAsync(uri, requestContent); await postTask.ConfigureAwait(false); postTask.Wait(); using (HttpResponseMessage response = postTask.Result) { if (!response.IsSuccessStatusCode) { throw new DevicePortalException(response); } if (response.Content != null) { using (HttpContent responseContent = response.Content) { responseDataStream = new MemoryStream(); Task copyTask = responseContent.CopyToAsync(responseDataStream); await copyTask.ConfigureAwait(false); copyTask.Wait(); // Ensure we return with the stream pointed at the origin. responseDataStream.Position = 0; } } } } return(responseDataStream); }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { // Open a GZipStream that writes to the specified output stream. using (GZipStream gzip = new GZipStream(stream, CompressionMode.Compress, true)) { // Copy all the input content to the GZip stream. await content.CopyToAsync(gzip); } }
private static async Task HandleContentAsync(ServerActionContext context, HttpContent content) { foreach (KeyValuePair <string, IEnumerable <string> > pair in content.Headers) { context.HttpContext.Response.Headers.Add(pair.Key, pair.Value.ToArray()); } await content.CopyToAsync(context.HttpContext.Response.Body); }
/// <summary> /// Submits the http delete request to the specified uri. /// </summary> /// <param name="uri">The uri to which the delete request will be issued.</param> /// <param name="allowRetry">Allow the Post to be retried after issuing a Get call. Currently used for CSRF failures.</param> /// <returns>Task tracking HTTP completion</returns> private async Task <Stream> Delete(Uri uri, bool allowRetry = true) { MemoryStream dataStream = null; WebRequestHandler requestSettings = new WebRequestHandler(); requestSettings.UseDefaultCredentials = false; requestSettings.Credentials = this.deviceConnection.Credentials; requestSettings.ServerCertificateValidationCallback = this.ServerCertificateValidation; using (HttpClient client = new HttpClient(requestSettings)) { this.ApplyHttpHeaders(client, HttpMethods.Delete); Task <HttpResponseMessage> deleteTask = client.DeleteAsync(uri); await deleteTask.ConfigureAwait(false); deleteTask.Wait(); using (HttpResponseMessage response = deleteTask.Result) { if (!response.IsSuccessStatusCode) { // If this isn't a retry and it failed due to a bad CSRF // token, issue a GET to refresh the token and then retry. if (allowRetry && this.IsBadCsrfToken(response)) { await this.RefreshCsrfToken(); return(await this.Delete(uri, false)); } throw new DevicePortalException(response); } this.RetrieveCsrfToken(response); if (response.Content != null) { using (HttpContent content = response.Content) { dataStream = new MemoryStream(); Task copyTask = content.CopyToAsync(dataStream); await copyTask.ConfigureAwait(false); copyTask.Wait(); // Ensure we return with the stream pointed at the origin. dataStream.Position = 0; } } } } return(dataStream); }
public static Task CopyToAsync(this HttpContent httpContent, Stream stream, CancellationToken token) { if (httpContent is null) { throw new ArgumentNullException(nameof(httpContent)); } return(httpContent.CopyToAsync(stream.InjectCancellationToken(token))); }
/// <summary> /// Initializes a new instance of the <see cref="DevicePortalException"/> class. /// </summary> /// <param name="responseMessage">Http response message.</param> /// <param name="message">Optional exception message.</param> /// <param name="innerException">Optional inner exception.</param> /// <returns>async task</returns> public static async Task <DevicePortalException> CreateAsync( HttpResponseMessage responseMessage, string message = "", Exception innerException = null) { DevicePortalException error = new DevicePortalException( responseMessage.StatusCode, responseMessage.ReasonPhrase, responseMessage.RequestMessage != null ? responseMessage.RequestMessage.RequestUri : null, message, innerException); try { if (responseMessage.Content != null) { Stream dataStream = null; using (HttpContent content = responseMessage.Content) { dataStream = new MemoryStream(); await content.CopyToAsync(dataStream).ConfigureAwait(false); // Ensure we point the stream at the origin. dataStream.Position = 0; } if (dataStream != null) { HttpErrorResponse errorResponse = dataStream.ReadJson <HttpErrorResponse>(); if (errorResponse != null) { error.HResult = errorResponse.ErrorCode; error.Reason = errorResponse.ErrorMessage; // If we didn't get the Hresult and reason from these properties, try the other ones. if (error.HResult == 0) { error.HResult = errorResponse.Code; } if (string.IsNullOrEmpty(error.Reason)) { error.Reason = errorResponse.Reason; } } } } } catch (Exception) { // Do nothing if we fail to get additional error details from the response body. } return(error); }
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { Stream compressedStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true); return(originalContent.CopyToAsync(compressedStream).ContinueWith(tsk => { compressedStream.Dispose(); })); }
/** * Get the SHA-256 hash of the POST body. * * @param request the request. * @return the canonicalized data, and the possibly updated request. * @throws RequestSigningException */ private CanonicalizerHelper GetContentHash(HttpRequestMessage request) { String data = ""; HttpRequestMessage updatedRequest = request; // only do hash for POSTs for this version if ("POST".Equals(request.Method.Method, StringComparison.InvariantCultureIgnoreCase)) { HttpContent content = request.Content; try { if (content != null) { MemoryStream memoryStream = new MemoryStream(); Task task = content.CopyToAsync(memoryStream); task.Wait(); if (memoryStream.Length > _maxBodySize) { throw new RequestSigningException("Content body too large."); } byte[] contentBytes = memoryStream.ToArray(); //if (LOGGER.isDebugEnabled()) //{ // LOGGER.debug(String.format("Content: %s", Base64.encodeBase64String(contentBytes))); //} byte[] digestBytes = GetHash(contentBytes); //if (LOGGER.isDebugEnabled()) //{ // LOGGER.debug(String.format("Content hash: %s", Base64.encodeBase64String(digestBytes))); //} // TODO - Do we need this?? // for non-retryable content, reset the content for downstream handlers //if (!content.retrySupported()) //{ // HttpContent newContent = new ByteArrayContent(content.getType(), contentBytes); // updatedRequest = request.setContent(newContent); //} data = Convert.ToBase64String(digestBytes); } } catch (IOException ioe) { throw new RequestSigningException("Failed to get content hash: failed to read content", ioe); } } return(new CanonicalizerHelper(data, updatedRequest)); }