Transfer-Encoding: chunked

Http1.1中 使用 chunked 编码传送时 没有CONTENT_LENGTH,下载之前无法确定要下载的大小. Wininet中已经内嵌该传输协议,要查看chunked块的大小只能socket底层编写获取报文. Wininet能不能直接访问原报文需走旁路.
// socket_http.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 

#pragma warning(disable:4996)
#pragma warning(disable:4267)
#pragma comment(lib, "WS2_32.lib")

void logFile(char *p,int len);
char fname[100];

int main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int bytesRecv;

	//Initialize Winsock
	wVersionRequested = MAKEWORD(2,2);
	bytesRecv = WSAStartup(wVersionRequested,&wsaData);

	if(bytesRecv != 0)
	{
		printf("WSAStartup failed: %d\n", bytesRecv);
		return 1;

	}

	if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();
		return 1;
	}
	strcpy(fname,"index.html");
	_unlink(fname);

	char host[] = {"www.baidu.com"};
	hostent * ht = gethostbyname(host);
	struct in_addr ip_addr;
	memcpy(&ip_addr,ht->h_addr_list[0],4);

	SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr = ip_addr;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(80);

	char ip[20];
	sprintf(ip,"%d.%d.%d.%d",
		addrSrv.sin_addr.S_un.S_un_b.s_b1,
		addrSrv.sin_addr.S_un.S_un_b.s_b2,
		addrSrv.sin_addr.S_un.S_un_b.s_b3,
		addrSrv.sin_addr.S_un.S_un_b.s_b4);
	printf("host:%s ip:%s\r\n",host,ip);

	bytesRecv = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
	if( bytesRecv == SOCKET_ERROR)
	{
		WSACleanup();
		return 1;
	}

	char recvBuf[1024];
	char* protocolHead= "GET / HTTP/1.1\n"
			"Host:www.baidu.com\n"
			"Accept:*/*\n"
			"User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\n"
			"Connection:close\n\n";

	bytesRecv = send(sockClient,protocolHead,strlen(protocolHead)+1,0);
	printf("request headers:\r\n%s\r\n",protocolHead);
	int ic=0;
	while( bytesRecv != SOCKET_ERROR )
	{
		memset(recvBuf,0,1024);
		bytesRecv = recv(sockClient,recvBuf,1024,0);
		if(ic==0)
			printf("response headers[%ld]:\r\n%s\r\n",bytesRecv,recvBuf);
		if(ic==1)
			printf("body [%ld]:\r\n%s",bytesRecv,recvBuf);
		ic++;
		if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
			printf( "Connection Closed.\n");
			break;
		}
		logFile(recvBuf,bytesRecv);
	}
	closesocket(sockClient);
	WSACleanup();

	return 0;
}

void logFile(char *p,int len)
{
	FILE *fp = fopen(fname,"a+");
	if(!fp) return;
	fwrite(p,1,len,fp);
	fclose(fp);
}

----------------------------------------------------------------------
运行结果
host:www.baidu.com ip:119.75.218.77

request headers:
GET / HTTP/1.1
Host:www.baidu.com
Accept:*/*
User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Connection:close

response headers[633]:
HTTP/1.1 200 OK
Date: Sun, 13 Apr 2014 18:24:51 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: Close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=10AA88D7B6D34E65F9FF8A384BE63FE9:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: H_PS_PSSID=5778_5991_1421_5225_5848_4760_6017_5856_5918; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Expires: Sun, 13 Apr 2014 18:24:20 GMT
X-Powered-By: HPHP
Server: BWS/1.1
BDPAGETYPE: 1
BDQID: 0x875baba2004f0570
BDUSERID: 0

body [1024]:
d27b  <--- chunked 大小
^^^^
以下是html正文
Connection Closed.
posted @ 2014-04-14 00:14  1CM  阅读(1153)  评论(1编辑  收藏  举报