Пример #1
0
        public async Task <FiHttpResult> Custom(String verb, String Url, Func <Stream, Task> UploadRequestStream = null)
        {
            if (UploadRequestStream == null || UseRequestBuffer)
            {
                byte[] bytes = new byte[0];
                if (UploadRequestStream != null)
                {
                    using (var ms = new MemoryStream()) {
                        await UploadRequestStream.Invoke(ms);

                        ms.Seek(0, SeekOrigin.Begin);
                        bytes = ms.ToArray();
                    }
                }
                var req = GetRequest(Url);
                req.Proxy     = this.Proxy;
                req.Method    = verb;
                req.UserAgent = UserAgent;
                UpdateSyncCode();
                AddHeaders(req);

                return(await FiHttpResult.Init(verb, req, bytes, UseResponseBuffer));
            }
            else
            {
                var req = GetRequest(Url);
                req.Proxy     = this.Proxy;
                req.Method    = verb;
                req.UserAgent = UserAgent;
                UpdateSyncCode();
                AddHeaders(req);
                return(await FiHttpResult.InitFromPost(req, UploadRequestStream, UseResponseBuffer));
            }
        }
Пример #2
0
        public async Task <FiHttpResult> Get(string Url)
        {
            var req = GetRequest(Url);

            req.Proxy     = this.Proxy;
            req.Method    = "GET";
            req.UserAgent = UserAgent;
            UpdateSyncCode();
            AddHeaders(req);
            return(await FiHttpResult.InitFromGet(req, UseResponseBuffer));
        }
Пример #3
0
        public async Task <FiHttpResult> Custom(String verb, String Url, byte[] data)
        {
            var req = GetRequest(Url);

            req.Proxy         = this.Proxy;
            req.ContentLength = data.Length;
            req.Method        = verb;
            req.UserAgent     = UserAgent;
            UpdateSyncCode();
            AddHeaders(req);

            return(await FiHttpResult.Init(verb, req, data, UseResponseBuffer));
        }
Пример #4
0
        public static async Task <FiHttpResult> InitFromGet(HttpWebRequest req, bool useBuffer)
        {
            var retv = new FiHttpResult(useBuffer);

            try {
                retv.Response = await req.GetResponseAsync();

                retv.Init(retv.Response);
            } catch (WebException wex) {
                retv.Response = wex.Response;
                retv.Init(retv.Response);
            }
            return(retv);
        }
Пример #5
0
        public static async Task <FiHttpResult> InitFromPost(HttpWebRequest req, Func <Stream, Task> UploadRequestStream, bool useBuffer)
        {
            var retv = new FiHttpResult(useBuffer);

            try {
                using (var reqStream = await req.GetRequestStreamAsync()) {
                    await UploadRequestStream?.Invoke(reqStream);
                }
                retv.Response = req.GetResponse();
                retv.Init(retv.Response);
            } catch (WebException wex) {
                retv.Response = wex.Response;
                retv.Init(retv.Response);
            }
            return(retv);
        }
Пример #6
0
        public static async Task <FiHttpResult> Init(string verb, HttpWebRequest req, byte[] data, bool useBuffer)
        {
            var retv = new FiHttpResult(useBuffer);

            try {
                if (verb != "GET" && verb != "OPTIONS")
                {
                    try {
                        req.ContentLength = data.Length;
                        using (var reqStream = await req.GetRequestStreamAsync()) {
                            var blen    = 2048;
                            var buff    = new byte[blen];
                            int chunksz = 0;
                            for (int i = 0; i *blen < data.Length; i++)
                            {
                                chunksz = Math.Min(blen, data.Length - blen * i);
                                Array.Copy(data, blen * i, buff, 0, chunksz);
                                await reqStream.WriteAsync(buff, 0, chunksz);
                            }
                            await reqStream.FlushAsync();

                            reqStream.Close();
                        }
                    } catch (Exception x) {
                        Debugger.Break();
                        throw x;
                    }
                }

                var resp = await req.GetResponseAsync();

                retv.Init(resp);
            } catch (WebException wex) {
                retv.Response = wex.Response;
                retv.Init(retv.Response);
            } catch (Exception x) {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                throw x;
            }
            return(retv);
        }