예제 #1
0
 public IAsyncOperationWithProgress <IInputStream, ulong> ReadAsInputStreamAsync()
 {
     return(AsyncInfo.Run <IInputStream, ulong>(async(token, progress) =>
     {
         return (await content.ReadAsStreamAsync()).AsInputStream();
     }));
 }
예제 #2
0
        public static async Task <object> ToStreamContentAsync(this SystemHttpContent source)
        {
            if (source is null)
            {
                return(null);
            }

            return(new StreamContent(await source.ReadAsStreamAsync()));
        }
예제 #3
0
 public static async Task <T> ReadAsAsync <T>(this System.Net.Http.HttpContent content) =>
 await System.Text.Json.JsonSerializer.DeserializeAsync <T>(await content.ReadAsStreamAsync());
예제 #4
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);
        }