Example #1
0
        public IAsyncOperationWithProgress <ulong, ulong> WriteToStreamAsync(IOutputStream outputStream)
        {
            return(AsyncInfo.Run <ulong, ulong>(async(token, progress) =>
            {
                Stream stream = outputStream.AsStreamForWrite();
                await content.CopyToAsync(stream);
                stream.Flush();

                return 0;
            }));
        }
Example #2
0
        public static async Task <System.Net.Http.HttpContent> CloneHttpContentAsync(System.Net.Http.HttpContent req)
        {
            if (req == null)
            {
                throw new ArgumentException("HTTPContent expected");
            }
            System.Net.Http.HttpContent clone;

            // Copy the request's content (via a MemoryStream) into the cloned object

            System.IO.Stream ms;
            long             contentLen = 99;

            if ((req.Headers != null) && (req.Headers.ContentLength != null))
            {
                contentLen = (long)req.Headers.ContentLength;
            }
            if (contentLen > (50d * OneMBInBytes))
            {
                string tempFN = System.IO.Path.GetTempFileName();
                ms = GetDeleteOnCloseTempFileStream(  );
            }
            else
            {
                ms = new System.IO.MemoryStream( );
            }

            await req.CopyToAsync(ms).ConfigureAwait(false);

            ms.Position = 0;
            clone       = new System.Net.Http.StreamContent(ms);

            // Copy the content headers
            if (req.Headers != null)
            {
                foreach (var h in req.Headers)
                {
                    clone.Headers.Add(h.Key, h.Value);
                }
            }

            return(clone);
        }
Example #3
0
        private static async System.Threading.Tasks.Task SystemNetAsync(ServiceResponse response, string method, Uri uri, List <Tuple <string, string> > headers, System.Net.Http.HttpContent requestContent, bool binaryResponse, int maxResponseLength)
        {
            long t = 0;

            response.stopWatch.Start();
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);

                request.Method = method;


                if (requestContent != null)
                {
                    if (requestContent.GetType() == typeof(System.Net.Http.MultipartFormDataContent))
                    {
                        request.ContentType = requestContent.Headers.ContentType.ToString();
                    }
                    using (System.IO.Stream requestStream = await request.GetRequestStreamAsync())
                    {
#if false
                        System.IO.Stream s = await requestContent.ReadAsStreamAsync();

                        long?l = requestContent.Headers.ContentLength;
                        if (l > int.MaxValue)
                        {
                            throw new ArgumentOutOfRangeException();
                        }
                        int    i      = (int)l;
                        byte[] buffer = new byte[i];
                        int    len    = await s.ReadAsync(buffer, 0, i);

                        //System.IO.StreamReader sr = new System.IO.StreamReader(requestContent.ReadAsStreamAsync());
                        //await requestContent.CopyToAsync(sr);
#endif
                        await requestContent.CopyToAsync(requestStream);
                    }
                }

                if (headers != null)
                {
                    foreach (Tuple <string, string> h in headers)
                    {
                        switch (h.Item1)
                        {
                        case "Accept":     // must use explicity Accept property otherwise an error is thrown
                            request.Accept = h.Item2;
                            break;

                        case "Content-Type":     // must use explicity ContentType property otherwise an error is thrown
                            request.ContentType = h.Item2;
                            break;

                        case "User-Agent":     // must use explicity ContentType property otherwise an error is thrown
                            request.UserAgent = h.Item2;
                            break;

                        default:
                            request.Headers[h.Item1] = h.Item2;
                            break;
                        }
                    }
                }

                t = response.stopWatch.ElapsedMilliseconds;
                using (System.Net.WebResponse wr = await request.GetResponseAsync())
                {
                    response.RequestElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds - t;
                    Log.WriteLine("Request Elapsed milliseconds:" + response.RequestElapsedMilliseconds);
                    using (System.Net.HttpWebResponse hwr = (System.Net.HttpWebResponse)wr)
                    {
                        response.StatusCode = (int)hwr.StatusCode;
                        Log.WriteLine("Request StatusCode:" + response.StatusCode);
                        if (hwr.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            response.ResponseJson = null;
                            using (System.IO.Stream rr = wr.GetResponseStream())
                            {
                                if (binaryResponse)
                                {
                                    await PostSystemNetAsyncBinaryReader(rr, maxResponseLength, response);
                                }
                                else
                                {
                                    await PostSystemNetAsyncStreamReader(rr, response);
                                }
                            }
                        }
                        else
                        {
                            response.ResponseResult = hwr.StatusDescription;
                            Log.WriteLine("PostAsync Failed: StatusCode:" + hwr.StatusDescription + "(" + response.StatusCode.ToString() + ")");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLine("Exception:" + ex.Message);
                if (ex.InnerException != null)
                {
                    Log.WriteLine("InnerException:" + ex.InnerException);
                }
            }
            response.FileName = "curl-" + httpCallCount;
            response.stopWatch.Stop();
            response.TotalElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds;
            Log.WriteLine("Total Elapsed milliseconds:" + response.TotalElapsedMilliseconds);
        }