示例#1
0
        public static string SendStringToUrl(this string url, string method        = null,
                                             string 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.GetString(webReq));
            }

            if (requestBody != null)
            {
                using (var reqStream = webReq.GetRequestStream())
                    using (var writer = new StreamWriter(reqStream))
                    {
                        writer.Write(requestBody);
                    }
            }

            using (var webRes = webReq.GetResponse())
                using (var stream = webRes.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        if (responseFilter != null)
                        {
                            responseFilter((HttpWebResponse)webRes);
                        }
                        return(reader.ReadToEnd());
                    }
        }
示例#2
0
        public static string SendStringToUrl(this string url, string method        = null,
                                             string 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.GetString(webReq));
            }

            if (requestBody != null)
            {
                using (var reqStream = PclExport.Instance.GetRequestStream(webReq))
                    using (var writer = new StreamWriter(reqStream))
                    {
                        writer.Write(requestBody);
                    }
            }

            using (var webRes = PclExport.Instance.GetResponse(webReq))
                using (var stream = webRes.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        if (responseFilter != null)
                        {
                            responseFilter((HttpWebResponse)webRes);
                        }
                        return(reader.ReadToEnd());
                    }
        }