Two ways to send an HTTP request
Last Update:2017-02-28
Source: Internet
Author: User
The request has written two Tests.
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");
Todo
{
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 ();
}
}
}
Here's the result.
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 bytes to Server
http/1.1 OK
server:microsoft-iis/5.0
Date:tue, Mar 2002 20:04:23 GMT
Pragma:no-cache
Cache-control:private
Connection:keep-alive
content-length:2025
Content-type:text/html
Expires:mon, Mar 2002 20:04:22 GMT
SET-COOKIE:ASPSESSIONIDGQQQQVCK=GELPBBIBLDCMOIOEAIBHINMB; path=/
Cache-control:no-cache
<!--
-Dev-club.com Developer Club
-Based on Active BBS 5.0 by shenpengfei@hotmail.com
-Last Updated:2001-12-1
-->
<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> Computer World Developer Club </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>
<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>
http/1.1 OK
server:microsoft-iis/5.0
Date:tue, Mar 2002 20:04:23 GMT
Pragma:no-cache
Cache-control:private
Connection:keep-alive
content-length:2025
Content-type:text/html
Expires:mon, Mar 2002 20:04:22 GMT
SET-COOKIE:ASPSESSIONIDGQQQQVCK=GELPBBIBLDCMOIOEAIBHINMB; path=/
Cache-control:no-cache
<!--
-Dev-club.com Developer Club
-Based on Active BBS 5.0 by shenpengfei@hotmail.com
-Last Updated:2001-12-1
-->
<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> Computer World Developer Club </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>
<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>
The difference between the two is only the first information, speed does not realize how much different