获取指定url的请求内容

如有不明白的地方欢迎加QQ群 14670545 探讨
///<summary>
        /// 获取指定url的请求内容
        ///</summary>
        ///<param name="Url"></param>
        ///<param name="encode"></param>
        ///<returns></returns>
        public static string GetRemoteHtmlCodeByEncoding(string Url, string encode)
        {
            HttpWebResponse Result = null;

            try
            {
                HttpWebRequest Req = (HttpWebRequest) System.Net.WebRequest.Create(Url);
                Req.Method = "get";
                Req.ContentType = "application/x-www-form-urlencoded";
                Req.Credentials = CredentialCache.DefaultCredentials;
                Result = (HttpWebResponse) Req.GetResponse();

                StreamReader ReceiveStream = new StreamReader(Result.GetResponseStream(), Encoding.GetEncoding(encode));
                string OutPutString;
                try
                {
                    OutPutString = ReceiveStream.ReadToEnd();
                }
                catch
                {
                    OutPutString = string.Empty;
                }
                finally
                {
                    ReceiveStream.Close();
                }

                return OutPutString;
            }
            catch
            {
                return string.Empty;
            }
            finally
            {
                if (Result != null)
                {
                    Result.Close();
                }
            }
        }


/// <summary>
        /// 以POST方式抓取远程页面内容
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <param name="postData">参数列表</param>
        /// <param name="encodeType">编码类型</param>
        /// <returns></returns>
        public static string Post_Http(string url, string postData, string encodeType)
        {
            HttpWebResponse myResponse = null;
            string strResult = null;
            try
            {
                Encoding encoding = Encoding.GetEncoding(encodeType);
                byte[] POST = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = POST.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(POST, 0, POST.Length); //设置POST
                newStream.Close();
                // 获取结果数据
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                strResult = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
            }
            finally
            {
                if (myResponse != null)
                    myResponse.Close();
            }
            return strResult;
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值