发送HTTP请求的两种方式

80酷酷网    80kuku.com

  请求写了两个测试了一下,自己看看吧

public void GetResponse()
        {
            StringBuilder strSource = new StringBuilder();
            try
            {
                //WebRequest Wreq = WebRequest.Create("http://www.dev-club.com");
                //WebResponse Wresp = Wreq.GetResponse();

                Uri myUri = new Uri(this.l_strPageAddress);
                HttpWebResponse resp;
                HttpWebRequest req =
                    (HttpWebRequest)WebRequest.Create(myUri);
                resp = (HttpWebResponse)req.GetResponse();


                StreamReader sr = new StreamReader(resp.GetResponseStream(),Encoding.Default);
                string strTemp = "";

                while((strTemp = sr.ReadLine()) != null)
                {
                    strSource.Append(strTemp + "\r\n");
                }

                sr.Close();
            }
            catch(WebException WebExcp)
            {
                strSource.Append(WebExcp.Message);
            }
            this.form.AddMessage(strSource.ToString());
        }

        public void Socket_GetResponse()
        {
            

                IPHostEntry ipinfo    = Dns.Resolve(l_strServerAddress);
                IPAddress ip        = ipinfo.AddressList[0];
                IPEndPoint rip        = new IPEndPoint(ip,80);    

                s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

                try
                {

                this.form.AddMessage("Connecting to "+ip.ToString());

                s.Connect(rip);

                int bytesRecv = 0;
                string output = "";
                string temp   = "";
                byte[] bytes;

                this.form.AddMessage("Sending request to "+l_strPageAddress);

                string Command = "post " +l_strPageAddress+" HTTP/1.0\r\n";
                Command += "Accept:*/*\r\n";
                Command += "Accept:text/html\r\n";
                Command += "\r\n";

                byte[] msg = Encoding.Default.GetBytes(Command);


                int intSend = s.Send(msg,msg.Length,0);

                this.form.AddMessage("Sent "+intSend.ToString()+" bytes to server");

                do
                {
                    bytes = new byte[1024];
                    bytesRecv = s.Receive(bytes,bytes.Length,SocketFlags.None);
                    temp = Encoding.Default.GetString(bytes,0,bytesRecv);
                    this.form.AddMessage(temp);
                    output += temp;
                }
                while (bytesRecv>0);

                this.form.AddMessage(output);
            }
            catch(Exception e)
            {
                this.form.AddMessage(e.Message);
            }
            finally
            {
                if (s != null & s.Connected)
                {
                    s.Shutdown(SocketShutdown.Both);
                    s.Close();
                }
            }

        }

下面是结果

Response from remote
Connecting to 61.135.135.13
Sending request to http://dev-club.ccw.com.cn/club/bbs/bbsView.asp?essenceID=18292
Sent 111 bytes to server
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 19 Mar 2002 20:04:23 GMT
pragma: no-cache
cache-control: private
Connection: Keep-Alive
Content-Length: 2025
Content-Type: text/html
Expires: Mon, 18 Mar 2002 20:04:22 GMT
Set-Cookie: ASPSESSIONIDGQQQQVCK=GELPBBIBLDCMOIOEAIBHINMB; path=/
Cache-control: no-cache

<!--
    - Dev-club.com开发者俱乐部
    - Based on Active BBS 5.0 By shenpengfeihotmail.com
    - Last Updated: 2001-12-1
-->

<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=gb2312">
<link REL="SHORTCUT ICON" language="javascript" src="js/rollover.js">
</script>
<script language="javascript">
    var bLoad=false;
    
    function doRefresh(){
        self.frames[1].location="refresh.asp
"
        setTimeout("doRefresh()",60000)
    }
    function init(){
        doRefresh()
        //MM_preloadImages('Images/head_about_on.gif','Images/head_course_on.gif','Images/head_partner_on.gif','Images/head_news_on.gif','Images/head_continue_on.gif','Images/head_job_on.gif');
    }
    var webSiteID = 6;window.onload = doRefresh
</script>
<script language="javascript1.1" src="js/dcookie.js"></script>
<script>
if (do
cument.all==null) window.location.name=forum rows="28,*" noresize scrolling="no" border=0 frameborder="0"  bordercolor="white" framespacing="0" >
<frame name=title src="title.asp" noresize scrolling="no" >
<frameset name=content cols="150,*" noresize scrolling="no" border=0 frameborder="0" framespacing="0">
    <frameset name=menu noresize scrolling="no" border=0 frameborder="0" framespacing="0" rows="20,*">
        <frame name="BoardRefresh" noresize scrolling="no" src="">
        <frame name="BoardTitle" noresize scrolling="no" target="BoardMenu" src="menu.asp">
    </frameset>
    <frame src="main.asp?essenceid=18292" name="BoardMain">
</frameset>
</frameset>
<noframes>
<body>
  sorry ! your browser not support frame!
</body>
</noframes>

</html>


HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 19 Mar 2002 20:04:23 GMT
pragma: no-cache
cache-control: private
Connection: Keep-Alive
Content-Length: 2025
Content-Type: text/html
Expires: Mon, 18 Mar 2002 20:04:22 GMT
Set-Cookie: ASPSESSIONIDGQQQQVCK=GELPBBIBLDCMOIOEAIBHINMB; path=/
Cache-control: no-cache

<!--
    - Dev-club.com开发者俱乐部
    - Based on Active BBS 5.0 By shenpengfeihotmail.com
    - Last Updated: 2001-12-1
-->

<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=gb2312">
<link REL="SHORTCUT ICON" language="javascript" src="js/rollover.js">
</script>
<script language="javascript">
    var bLoad=false;
    
    function doRefresh(){
        self.frames[1].location="refresh.asp"
        setTimeout("doRefresh()",60000)
    }
    function init(){
        doRefresh()
        //MM_preloadImages('Images/head_about_on.gif','Images/head_course_on.gif','Images/head_partner_on.gif','Images/head_news_on.gif','Images/head_continue_on.gif','Images/head_job_on.gif');
    }
    var webSiteID = 6;window.onload = doRefresh
</script>
<script language="javascript1.1" src="js/dcookie.js"></script>
<script>
if (document.all==null) window.location.name=forum rows="28,*" noresize scrolling="no" border=0 frameborder="0"  bordercolor="white" framespacing="0" >
<frame name=title src="title.asp" noresize scrolling="no" >
<frameset name=content cols="150,*" noresize scrolling="no" border=0 frameborder="0" framespacing="0">
    <frameset name=menu noresize scrolling="no" border=0 frameborder="0" framespacing="0" rows="20,*">
        <frame name="BoardRefresh" noresize scrolling="no" src="">
        <frame name="BoardTitle" noresize scrolling="no" target="BoardMenu" src="menu.asp">
    </frameset>
    <frame src="main.asp?essenceid=18292" name="BoardMain">
</frameset>
</frameset>
<noframes>
<body>
  sorry ! your browser not support frame!
</body>
</noframes>

</html>

两者只相差头信息而已,速度体会不出多大的差别

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: