首页 > 开发 > 综合 > 正文

发送HTTP请求的两种方式

2024-07-21 02:16:56
字体:
来源:转载
供稿:网友


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

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 [email protected]
    - last updated: 2001-12-1
-->

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link rel="shortcut icon" href="http://www.chinaasp.com/icon.ico">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>计算机世界开发者俱乐部</title>
<style>
.navpoint
    {
    font-family: webdings;
    font-size:9pt;
    color:black;
    cursor:hand;
    }
p{
    font-size:9pt;
}
</style>
<script 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');
    }
    window.onload = dorefresh
</script>
<script language="javascript1.1" src="js/dcookie.js"></script>
<script>
if (do
cument.all==null) window.location.href="notieview.asp?essenceid=18292"
function monitor(){
window.open('usermonitor.asp','_blank','left=3000,top=4000')
}
//window.onunload = monitor
</script>
</head>
<frameset 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 [email protected]
    - last updated: 2001-12-1
-->

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link rel="shortcut icon" href="http://www.chinaasp.com/icon.ico">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>计算机世界开发者俱乐部</title>
<style>
.navpoint
    {
    font-family: webdings;
    font-size:9pt;
    color:black;
    cursor:hand;
    }
p{
    font-size:9pt;
}
</style>
<script 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');
    }
    window.onload = dorefresh
</script>
<script language="javascript1.1" src="js/dcookie.js"></script>
<script>
if (document.all==null) window.location.href="notieview.asp?essenceid=18292"
function monitor(){
window.open('usermonitor.asp','_blank','left=3000,top=4000')
}
//window.onunload = monitor
</script>
</head>
<frameset 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>

两者只相差头信息而已,速度体会不出多大的差别
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表