使用linux函数模拟HTTP 请求过程

非常好的参考资料

1.HTTP 协议资料

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2

2.使用telnet模拟浏览器访问网页

http://pcwanli.blog.163.com/blog/static/4531561120091115105240732/

 

 

#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#include <iostream>
using namespace std;

#define MAXADDRLEN 256
#define BUFLEN 128
#define MAXSLEEP 128
int connect_retry(int sockfd,const struct sockaddr* addr,socklen_t alen){
	int nsec;
	for(nsec =1; nsec <= MAXSLEEP;nsec <<=1){
		if(connect(sockfd,addr,alen)==0){
			return 0;

		}else {

			if(nsec <=MAXSLEEP/2){
				sleep(nsec);
			}
		}


	}
	return -1;
}
int main(){
	struct addrinfo hint;
	struct addrinfo *ailist;
	int err =0;	
	hint.ai_flags = 0;
	hint.ai_family =0;
	hint.ai_socktype = SOCK_STREAM;
	hint.ai_protocol = 0;
	hint.ai_addrlen =0;
	hint.ai_canonname = NULL;
	hint.ai_addr = NULL;
	hint.ai_next = NULL;
	if((err = getaddrinfo("www.baidu.com","80",&hint,&ailist))!=0){
		err_quit("getaddrinfo error:%s",gai_strerror(err));

	}
	struct addrinfo* aip = ailist;
	int sockfd = 0;
	if((sockfd = socket(aip->ai_family,SOCK_STREAM,0))<0){
		err = errno;	
		cout<<"ok"<<endl;
	}
	if(connect_retry(sockfd,aip->ai_addr,aip->ai_addrlen)<0){
		cout<<"ok2"<<endl;
		err = errno;
	}
	//connect ok
	//send request
	const char* reqs[]={"GET /index.htm HTTP/1.1\n","User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5\n","host:www.baidu.com\n","Accept-Charset:UTF-8\n","connection:close\n","\n"};
//	const char* req = "GET /index.htm HTTP/1.0\n\n";
	const char* req;
	for(int i=0;i<6;i++){
		req=reqs[i];
		send(sockfd,req,strlen(req),0);

	}
//	send(sockfd,req,strlen(req),0);
	char buf[BUFLEN];
	int n=0;
	while((n=recv(sockfd,buf,BUFLEN,0))>0){
		write(STDOUT_FILENO,buf,n);
	}
	cout<<"ok"<<endl;
	cout<<endl;
	close(sockfd);
}

 如需编译还需要《unix环境高级编程》的apue.h 和error.c

通过以上程序我们可以了解到

1.浏览器是通过User-Agent字段向服务器表明自己的身份的

2.通过实验表明http1.1协议下默认连接为长连接,且recv()默认为阻塞函数

    如果不设置connection:close此属性,则会在recv()上阻塞一段时间。

   是否我应该这样认为,也就是说一直没有收到字节流的终止标志。

 

 

获得的http响应

 

HTTP/1.1 200 OK
Date: Wed, 08 Aug 2012 06:45:00 GMT
Server: BWS/1.0
Content-Length: 8225
Content-Type: text/html;charset=gbk
Cache-Control: private
Expires: Wed, 08 Aug 2012 06:45:00 GMT
Set-Cookie: BAIDUID=D0607B2A1D830DE33C4A566154E9937B:FG=1; expires=Wed, 08-Aug-42 06:45:00 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Close

<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>°Ù¶Èһϣ¬ÄãŸÍÖªµÀ      </title><style>html{overflow-y:auto}body{font:12px arial;text-align:center;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{color:#999;padding:4px 10px 5px 0;text-align:right}#u a{margin:0 5px}#u .reg{margin:0}#m{width:680px;margin:0 auto;}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px;}.s_ipt_wr{width:418px;height:30px;display:inline-block;margin-right:5px;background:url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png) no-repeat -304px 0;border:1px solid #b6b6b6;border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;vertical-align:top}.s_ipt{width:405px;height:22px;font:16px/22px arial;margin:5px 0 0 7px;background:#fff;outline:none;-webkit-appearance:none}.s_btn{width:95px;height:32px;padding-top:2px\9;font-size:14px;background:#ddd url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png);cursor:pointer}.s_btn_h{background-position:-100px 0}.s_btn_wr{width:97px;height:34px;display:inline-block;background:url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png) no-repeat -202px 0;*position:relative;z-index:0;vertical-align:top}#lg img{vertical-align:top;margin-bottom:3px}#lk{margin:33px 0}#lk span{font:14px "ËÎÌå"}#lm{height:60px}#lh{margin:16px 0 5px;word-spacing:3px}.tools{position:absolute;top:-4px;*top:10px;right:-13px;}#mHolder{width:62px;position:relative;z-index:296;display:none}#mCon{height:18px;line-height:18px;position:absolute;cursor:pointer;padding:0 18px 0 0;background:url(http://s1.bdstatic.com/r/www/img/bg-1.0.0.gif) no-repeat right -134px;background-position:right -136px\9}#mCon span{color:#00c;cursor:default;display:block}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none;filter:none\9}#mMenu,#user ul{box-shadow:1px 1px 2px #ccc;-moz-box-shadow:1px 1px 2px #ccc;-webkit-box-shadow:1px 1px 2px #ccc;filter: progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=135, Color="#cccccc")\9;}#mMenu{width:56px;border:1px solid #9b9b9b;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a:hover{background:#ebebeb}#mMenu .ln{height:1px;background:#ebebeb;overflow:hidden;font-size:1px;line-height:1px;margin-top:-1px}#cp,#cp a{color:#77c}#seth{display:none;behavior:url(#default#homepage)}#setf{display:none;}#sekj{margin-left:14px;}</style>
<script type="text/javascript">function h(obj){obj.style.behavior='url(#default#homepage)';var a = obj.setHomePage('http://www.baidu.com/');}</script></head>

<body><div id="u"><a href="http://www.baidu.com/gaoji/preferences.html" name="tj_setting">ËÑË÷ÉèÖÃ</a>|<a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" οnclick="return false;">µÇÂŒ</a><a href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" target="_blank" name="tj_reg" class="reg">×¢²á</a></div>
<div id="m"><p id="lg"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129"></p>
<p id="nv"><a href="http://news.baidu.com">ÐÂ&nbsp;ÎÅ</a>¡¡<b>Íø&nbsp;Ò³</b>¡¡<a href="http://tieba.baidu.com">Ìù&nbsp;°É</a>¡¡<a href="http://zhidao.baidu.com">Öª&nbsp;µÀ</a>¡¡<a href="http://mp3.baidu.com">MP3</a>¡¡<a href="http://image.baidu.com">ÍŒ&nbsp;Ƭ</a>¡¡<a href="http://video.baidu.com">ÊÓ&nbsp;Ƶ</a>¡¡<a href="http://map.baidu.com">µØ&nbsp;ÍŒ</a></p><div id="fm"><form name="f" action="/s"><span class="s_ipt_wr"><input type="text" name="wd" id="kw" maxlength="100" class="s_ipt"></span><input type="hidden" name="rsv_bp" value="0"><input type="hidden" name="rsv_spt" value="3"><span class="s_btn_wr"><input type="submit" value="°Ù¶ÈÒ»ÏÂ" id="su" class="s_btn" οnmοusedοwn="this.className='s_btn s_btn_h'" οnmοuseοut="this.className='s_btn'"></span></form><span class="tools"><span id="mHolder"><div id="mCon"><span>ÊäÈë·š</span></div></span></span><ul id="mMenu"><li><a href="#" name="ime_hw">ÊÖÐŽ</a></li><li><a href="#" name="ime_py">ÆŽÒô</a></li><li class="ln"></li><li><a href="#" name="ime_cl">¹Ø±Õ</a></li></ul></div>
<p id="lk"><a href="http://baike.baidu.com">°Ù¿Æ</a>¡¡<a href="http://wenku.baidu.com">ÎÄ¿â</a>¡¡<a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">žü¶à&gt;&gt;</a></span></p><p id="lm"></p><p ><a id="seth" onClick="h(this)" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" οnmοusedοwn="return ns_c({'fm':'behs','tab':'homepage','pos':0})">°Ñ°Ù¶ÈÉèΪÖ÷Ò³</a><a id="setf" href="http://www.baidu.com/cache/sethelp/index.html" οnmοusedοwn="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">°Ñ°Ù¶ÈÉèΪÖ÷Ò³</a><span id="sekj"><a href="http://www.baidu.com/search/baidukuijie_mp.html" target="_blank" οnmοusedοwn="return ns_c({'fm':'behs','tab':'kuaijie','pos':1})">°Ñ°Ù¶ÈÌíŒÓµœ×ÀÃæ</a></span></p>
<p id="lh"><a href="http://e.baidu.com/?refer=888">ŒÓÈë°Ù¶ÈÍÆ¹ã</a> | <a href="http://top.baidu.com">ËÑË÷·çÔÆ°ñ</a> | <a href="http://home.baidu.com">¹ØÓÚ°Ù¶È</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">&copy;2012 Baidu <a href="/duty/">ʹÓðٶÈǰ±Ø¶Á</a> <a href="http://www.miibeian.gov.cn" target="_blank">Ÿ©ICPÖ€030173ºÅ</a> <img src="http://www.baidu.com/cache/global/img/gs.gif"></p></div></body>

<script>var bds={se:{},comm : {ishome : 1,sid : "1324",user : "",username : "",sugHost : "http://suggestion.baidu.com/su",loginAction : []}}</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/home-1.2.js"></script><script>var bdUser = null;var w=window,d=document,n=navigator,k=d.f.wd,a=d.getElementById("nv").getElementsByTagName("a"),isIE=n.userAgent.indexOf("MSIE")!=-1&&!window.opera;for(var i=0;i<a.length;i++){a[i].οnclick=function(){if(k.value.length>0){var o=this,h=o.href,q=encodeURIComponent(k.value);if(h.indexOf("q=")!=-1){o.href=h.replace(/q=[^&\x24]*/,"q="+q)}else{this.href+="?q="+q}}}};(function(){if(/q=([^&]+)/.test(location.search)){k.value=decodeURIComponent(RegExp["\x241"])}})();if(n.cookieEnabled&&!/sug?=0/.test(d.cookie)){bds.se.sug();};function addEV(o, e, f){if(w.attachEvent){o.attachEvent("on" + e, f);}else if(w.addEventListener){ o.addEventListener(e, f, false);}}function G(id){return d.getElementById(id);}function ns_c(q){var p = encodeURIComponent(window.document.location.href), sQ = '', sV = '', mu='', img = window["BD_PS_C" + (new Date()).getTime()] = new Image();for (v in q) {sV = q[v];sQ += v + "=" + sV + "&";} mu= "&mu=" + p ;img.src = "http://nsclick.baidu.com/v.gif?pid=201&pj=www&rsv_sid=1324&" + sQ + "path="+p+"&t="+new Date().getTime();return true;}if(/\bbdime=[12]/.test(d.cookie)){document.write('<script src=http://s1.bdstatic.com/r/www/cache/ime/js/openime-1.0.0.js><\/script>');}(function(){var u = G("u").getElementsByTagName("a"), nv = G("nv").getElementsByTagName("a"), lk = G("lk").getElementsByTagName("a"), un = "";var tj_nv = ["news","tieba","zhidao","mp3","img","video","map"];var tj_lk = ["baike","wenku","hao123","more"];un = bds.comm.user == "" ? "" : bds.comm.user;function _addTJ(obj){addEV(obj, "mousedown", function(e){var e = e || window.event;var target = e.target || e.srcElement;ns_c({'fm':'behs','tab':target.name||'tj_user','un':encodeURIComponent(un)});});}for(var i = 0; i < u.length; i++){_addTJ(u[i]);}for(var i = 0; i < nv.length; i++){nv[i].name = 'tj_' + tj_nv[i];_addTJ(nv[i]);}for(var i = 0; i < lk.length; i++){lk[i].name = 'tj_' + tj_lk[i];_addTJ(lk[i]);}})();addEV(w,"load",function(){k.focus()});w.οnunlοad=function(){};</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/tangram-1.3.4c1.0.js"></script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/user/js/u-1.3.2.js"></script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/aoyun/js/aoyun-1.2.js"></script>



</html>



<!--f080b8037bcb957e-->
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值