发送HTTP请求的两种方式
发表于:2007-06-30来源:作者:点击数:
标签:
写了两个 测试 了一下,自己看看吧 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(t
写了两个
测试了一下,自己看看吧
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.Shu
tdown(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 shenpengfei@hotmail.com
- 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 shenpengfei@hotmail.com
- 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>
两者只相差头信息而已,速度体会不出多大的差别
原文转自:http://www.ltesting.net