C# 获取Post或Get返回的网页

来源:互联网 发布:windows怎么取消共享 编辑:程序博客网 时间:2024/06/15 07:52
/// <summary>
        /// 获取POST返回的网页代码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="body">传递的参数,格式"roleId=1&uid=2"</param>
        /// <param name="contentType">post的cotentType填写:"application/x-www-form-urlencoded" soap填写:"text/xml; charset=utf-8"</param>
        /// <returns></returns>
        public static string PostHttp(string url, string body = "", string contentType = "application/x-www-form-urlencoded")
        {
            System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);


            httpWebRequest.ContentType = contentType;
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;


            byte[] btBodys = System.Text.Encoding.UTF8.GetBytes(body);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);


            System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
            System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();


            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();


            return responseContent;
        }


        /// <summary>
        /// 获取Get返回的网页代码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="httpContext">传递的参数,格式"roleId=1&uid=2"</param>
        /// <returns></returns>
        public static string GetHttp(string url, HttpContext httpContext)
        {
            string queryString = "?";


            if (httpContext != null)
            {
                foreach (string key in httpContext.Request.QueryString.AllKeys)
                {
                    queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
                }
            }


            queryString = queryString.Substring(0, queryString.Length - 1);


            System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url + queryString);


            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "GET";
            httpWebRequest.Timeout = 20000;
            httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
            //byte[] btBodys = Encoding.UTF8.GetBytes(body);
            //httpWebRequest.ContentLength = btBodys.Length;
            //httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);


            System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
            System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();


            httpWebResponse.Close();
            streamReader.Close();


            return responseContent;
        }
0 0
原创粉丝点击