asp.net 获取远程网页内容

    #region 获取指定远程网页内容

    /// <summary>

    /// 获取指定远程网页内容

    /// </summary>

    /// <param name="strUrl">所要查找的远程网页地址</param>

    /// <param name="timeout">超时时长设置,一般设置为8000</param>

    /// <param name="enterType">是否输出换行符,0不输出,1输出文本框换行</param>

    /// <param name="EnCodeType">编码方式</param>

    /// <returns></returns>

    /// 也可考虑 static string

    public string GetRequestString(string strUrl, int timeout, int enterType, Encoding EnCodeType)
    {

        string strResult;

        try
        {

            HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);

            myReq.Timeout = timeout;

            HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();

            Stream myStream = HttpWResp.GetResponseStream();

            StreamReader sr = new StreamReader(myStream, EnCodeType);

            StringBuilder strBuilder = new StringBuilder();

            while (-1 != sr.Peek())
            {

                strBuilder.Append(sr.ReadLine());

                if (enterType == 1)
                {

                    strBuilder.Append("\r\n");

                }

            }

            strResult = strBuilder.ToString();

        }

        catch (Exception err)
        {

            strResult = "请求错误:" + err.Message;

        }

        return strResult;

    }

    #endregion

转载于:https://www.cnblogs.com/suxvsheng/archive/2007/07/04/805936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值