public static byte[] SendBytesToUrl(this string url, string method        = null,
                                            byte[] requestBody                    = null, string contentType = null, string accept = "*/*",
                                            Action <HttpWebRequest> requestFilter = null, Action <HttpWebResponse> responseFilter = null)
        {
            var webReq = (HttpWebRequest)WebRequest.Create(url);

            if (method != null)
            {
                webReq.Method = method;
            }

            if (contentType != null)
            {
                webReq.ContentType = contentType;
            }

            webReq.Accept = accept;
            webReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            webReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            if (requestFilter != null)
            {
                requestFilter(webReq);
            }

            if (ResultsFilter != null)
            {
                return(ResultsFilter.GetBytes(webReq));
            }

            if (requestBody != null)
            {
                using (var req = webReq.GetRequestStream())
                {
                    req.Write(requestBody, 0, requestBody.Length);
                }
            }

            using (var webRes = webReq.GetResponse())
            {
                if (responseFilter != null)
                {
                    responseFilter((HttpWebResponse)webRes);
                }

                using (var stream = webRes.GetResponseStream())
                {
                    return(stream.ReadFully());
                }
            }
        }
Exemple #2
0
        public static byte[] SendBytesToUrl(this string url, string method        = null,
                                            byte[] requestBody                    = null, string contentType = null, string accept = "*/*",
                                            Action <HttpWebRequest> requestFilter = null, Action <HttpWebResponse> responseFilter = null)
        {
            var webReq = (HttpWebRequest)WebRequest.Create(url);

            if (method != null)
            {
                webReq.Method = method;
            }

            if (contentType != null)
            {
                webReq.ContentType = contentType;
            }

            webReq.Accept = accept;
            PclExport.Instance.AddCompression(webReq);

            if (requestFilter != null)
            {
                requestFilter(webReq);
            }

            if (ResultsFilter != null)
            {
                return(ResultsFilter.GetBytes(webReq));
            }

            if (requestBody != null)
            {
                using (var req = PclExport.Instance.GetRequestStream(webReq))
                {
                    req.Write(requestBody, 0, requestBody.Length);
                }
            }

            using (var webRes = PclExport.Instance.GetResponse(webReq))
            {
                if (responseFilter != null)
                {
                    responseFilter((HttpWebResponse)webRes);
                }

                using (var stream = webRes.GetResponseStream())
                {
                    return(stream.ReadFully());
                }
            }
        }