Exemplo n.º 1
0
        public string getResponse(string url, HttpRequestMethod method = HttpRequestMethod.GET, Dictionary <string, string> postData = null, CookieContainer cookies = null)
        {
            string result         = "";
            var    httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.AllowAutoRedirect         = true;
            httpWebRequest.AllowWriteStreamBuffering = true;
            httpWebRequest.KeepAlive = true;
            httpWebRequest.Method    = method.ToString();

            if (cookies != null)
            {
                httpWebRequest.CookieContainer = cookies;
            }

            if (method == HttpRequestMethod.POST)
            {
                string queryString = QueryStringHelper.toQueryString(postData);
                byte[] b           = System.Text.Encoding.ASCII.GetBytes(queryString);
                httpWebRequest.ContentType   = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = (long)b.Length;
                Stream httpStream = httpWebRequest.GetRequestStream();
                httpStream.Write(b, 0, b.Length);
                httpStream.Close();
            }

            var objResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (var sr = new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();
                sr.Close();
            }
            return(result);
        }