|
- WSADATA wsaData;
- if( WSAStartup(MAKEWORD(2,0), &wsaData)
- || LOBYTE(wsaData.wVersion)!= 2 )
- return 0;
- ///创建SOCKET对象
- SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ); // ppe->p_proto
- if(sock == INVALID_SOCKET)
- return 0;
- ///根据主机名获得IP地址
- //hostent* pHostEnt=gethostbyname("www.sina.com.cn");
- //if(pHostEnt==NULL){
- // return 0;
- //}
- int nTime = 10000;
- setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTime, sizeof(nTime));
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTime, sizeof(nTime));
- ///连接
- //struct in_addr ip_addr;
- //memcpy(&ip_addr, pHostEnt->h_addr_list[0], 4);///h_addr_list[0]里4个字节,每个字节8位
- struct sockaddr_in destaddr;
- memset((void *)&destaddr, 0, sizeof(destaddr));
- destaddr.sin_family = AF_INET;
- destaddr.sin_port = htons([color=#FF0000]8080[/color]); //[color=#FF0000]此处是公司代理服务器的端口号[/color]
- destaddr.sin_addr.s_addr = inet_addr("[color=#FF0000]16.105.253.251[/color]");// = ip_addr; [color=#FF0000]公司只能用代理服务器 [/color]
- DWORD dwError;
- if( 0 != connect(sock, (struct sockaddr*)&destaddr, sizeof(destaddr)) )
- {
- dwError = GetLastError();
- return 0;
- }
- ///格式化请求
- char request[] =
- "GET http://sports.sina.com.cn/basketball/ HTTP/1.1\r\n"
- // "GET /book/2132/zip/gb.zip HTTP/1.1\r\n"
- // "Host:read.hjsm.net\r\n"
- "Host:sports.sina.com.cn\r\n"
- "Accept:*/*\r\n"
- //"Referer: http://sports.sina.com.cn/basketball.htm\r\n "
- //"User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n"
- //// "Range: bytes=0-\r\n"
- //"Pragma: no-cache\r\n"
- //"Cache-Control: no-cache\r\n"
- //"Connection:Close\r\n\r\n";
- "Connection:Keep-Alive\r\n\r\n";
- ///发送请求
- if( SOCKET_ERROR == send(sock, request, strlen(request), 0) )
- return 0;
- //---------Response----------
- // HTTP/1.1 200 OK
- // Date: Wed, 02 Feb 2005 08:42:09 GMT
- // Server: Apache
- // Last-Modified: Mon, 24 Jan 2005 13:17:07 GMT
- // ETag: "37a9ef-7635b-459bac0"
- // Accept-Ranges: bytes
- // Content-Length: 484187
- // Connection: close
- // Content-Type: application/zip
- // Transfer-Encoding: chunked - 当有该行存在时,content会是分块传送,每块有一个头,格式:"[16进制块大小,string]\r\n"
- int rcv_bytes = 0;
- char buf[2049] = {0,};
- //CFile file(_T("\\Windows\\File.txt"),CFile::modeReadWrite | CFile::modeCreate);
- while(1)
- {
- rcv_bytes =recv(sock, buf, 2048, 0);
- if( rcv_bytes <= 0 ) [color=#FF0000] //在这里断点看buf的内容[/color]
- break;
- //ofs.write(buf, rcv_bytes);
- //file.Write(buf,rcv_bytes);
- //break;
- }
- //file.Close();
- closesocket(sock);
- WSACleanup();
复制代码
上面是我的测试代码,除了修改了一下代理服务器的ip外(公司代理不能外传),全部没有改变。
|
|