首页 > 编程 > .NET > 正文

ASP.NET获取远程网页的内容之一

2024-07-10 13:12:01
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  •   一、本机直接上网时:

        #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

      二:通过域环境代理上网时这样就不行了! 下篇文章分解,呵呵!

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表