lenux:
你好,谢谢你。我刚试了,但是结果如下,仍是权力受限等
如下:
HTTP/1.1 200 OK
Date: Tue, 26 Aug 2008 06:56:02 GMT
Server: Apache/2.0.63 (Unix)
Last-Modified: Tue, 26 Aug 2008 06:46:53 GMT
ETag: "afb0-4555744e1b140"
Accept-Ranges: bytes
X-Powered-By: mod_xlayout_jh/0.0.1vhs.markII.remix
Cache-Control: max-age=60
Expires: Tue, 26 Aug 2008 06:57:02 GMT
Very: Accept-Encoding
Content-Type: text/html
X-Cache: HIT from us-cn4.sina.com.cn
Powered-By-ChinaCache: USA-NE-1-3D3 HIT
Content-length: 62497
Connection: Keep-Alive
.
.
.
.
therequested url could not be retrived
while tring to retrive the url:
http:/
/sports.sina.com.cn/basketball/
The following error was encountered;
-
access denied.
Access control configuration prevents your request from
being allowed at this time. please contact your service provider if you feel
this is incorrect
不知道是怎么回事?还麻烦你帮我看一下,是不是我的模拟器问题:代码如下:
WSADATA wsaData;
if( WSAStartup(MAKEWORD(1,1), &wsaData)
|| LOBYTE(wsaData.wVersion)!= 1 || HIBYTE(wsaData.wVersion)!= 1 )
return;
struct protoent *ppe;
ppe=getprotobyname("tcp");
///创建SOCKET对象
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ); // ppe->p_proto
if(sock == INVALID_SOCKET)
return;
///根据主机名获得IP地址
hostent* pHostEnt=gethostbyname("sports.sina.com.cn");
if(pHostEnt==NULL){
//MessageBox (_T ("This feature is currently unimplemented. Sorry!"),
// _T ("Error"), MB_ICONINFORMATION | MB_OK);
TCHAR szBuf[128];
int m_nWeight= WSAGetLastError();
wsprintf(szBuf,_T("%s (%d): %s"),
_T("错误是"), m_nWeight ,_T(" "));
MessageBox(szBuf,
_T ("Error"), MB_ICONINFORMATION | MB_OK);
return;
}
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(80);
destaddr.sin_addr = ip_addr;
if( 0 != connect(sock, (struct sockaddr*)&destaddr, sizeof(destaddr)) )
return;
///格式化请求
//char request[] =
//"GET http://sports.sina.com.cn/basketball/ HTTP/1.1\r\nHost:http://sports.sina.com.cn/basketball/\r\nAccept:*/*\r\n" ;
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;
}
//---------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 ){
break;
}
file.Write(buf,rcv_bytes);
break;
}
file.Close();
closesocket(sock);
WSACleanup();
或者麻烦你把你的代码发给我,我看一下也行,谢谢你! |