zoukankan      html  css  js  c++  java
  • 获取远程网页的内容之一

    一、本机直接上网时:
    #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二:通过域环境代理上网时这样就不行了!
  • 相关阅读:
    【转】UML中类与类之间的5种关系表示
    OSGI框架—HelloWorld小实例
    解决:“Workbench has not been created yet” error in eclipse plugin programming”,OSGI启动控制台报错问题
    Restful风格到底是什么?怎么应用到我们的项目中?
    Java程序员面试题集(1-50
    【转】Spring中@Component的作用
    【转】Spring AOP 实现原理与 CGLIB 应用
    【转】spring和springMVC的面试问题总结
    Java算法之“兔子问题”
    DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构
  • 原文地址:https://www.cnblogs.com/MaxIE/p/336000.html
Copyright © 2011-2022 走看看