Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP.NET > asp.net中获取远程网页的2种特殊方式
【标  题】:asp.net中获取远程网页的2种特殊方式
【关键字】:.net,远程网页,c#
【来  源】:http://www.cnandusa.com

asp.net中获取远程网页的2种特殊方式

Your Ad Here

//获取http页面函数
        private string Get_Http(string a_strUrl)
        {
            string strResult ;
            HttpWebRequest myReq = (HttpWebRequest)
                                   WebRequestFactory.Create(a_strUrl) ;

            try
            {
                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream () ;

                StreamReader sr = new StreamReader(myStream , Encoding.Default);
                StringBuilder strBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    strBuilder.Append(sr.ReadLine()+"\r\n");
                }
               
                strResult = strBuilder.ToString();
            }
            catch(Exception exp)
            {
                 strResult = "错误:" + exp.Message ;
            }

            return strResult ;
           
        }


        //通过同server建立tcp/ip连接,发送socket命令
        private string Get_Socket_Request(string a_strServer , string a_strRequest , Int32 a_intPort)
        {
            //Set up variables and String to write to the server
            Encoding ASCII = Encoding.Default ;
            string Get = a_strRequest + "Connection: Close\r\n\r\n";
            //string Get =
            Byte[] ByteGet = ASCII.GetBytes(Get);
            Byte[] RecvBytes = new Byte[256];
            String strRetPage = null;

            // IPAddress and IPEndPoint represent the endpoint that will
            //   receive the request
            IPAddress hostadd = DNS.Resolve(a_strServer.Substring(7 ,a_strServer.Length - 7));
            IPEndPoint EPhost = new IPEndPoint(hostadd, a_intPort);

            //Create the Socket for sending data over TCP
            Socket s = new Socket(AddressFamily.AfINet, SocketType.SockStream,
                                    ProtocolType.ProtTCP );

            // Connect to host using IPEndPoint
            if (s.Connect(EPhost) != 0)
            {
                strRetPage = "Unable to connect to host";
                return strRetPage;
            }

            // Sent the GET text to the host
            s.Send(ByteGet, ByteGet.Length, 0);

            // Receive the page, loop until all bytes are received
            Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
            strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

            while (bytes > 0)
            {
                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
                strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
            }

            return strRetPage ;
        }

XMLHTTP对象及其方法:【上一篇】
用宽代网架设游戏服务器:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • JFace Viewer 1
  • 用Photoshop制作马赛克效果
  • PVLAN配置
  • 惠普CSA证书到了:)
  • Emacs的info使用说明
  • 窗口最小化后在任务栏不能显示的问题
  • 在Linux使用即时通信软件:“msn“、”yahoo”
  • Google Analytics开始提供服务
  • Dojo.Book第一章翻译完成。(链接修正,抱歉)
  • 如何在SCO UNIX 5中直接运行DOS/WINDOWS程序
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.