65,210
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <afxinet.h>
int main(int argc, char* argv[])
{
CInternetSession session("HttpClient");
char * url = " http://www.imobile.com.cn/simcard.php?simcard=1392658";
CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
DWORD dwStatusCode;
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString content;
CString data;
while (pfile -> ReadString(data))
{
content += data + "\r\n";
}
content.TrimRight();
printf(" %s\n " ,(LPCTSTR)content);
}
pfile -> Close();
delete pfile;
session.Close();
return 0 ;
}
//VC6的代码
#include <Winsock2.h>
#include <stdio.h>
#include<fstream.h>
#pragma comment( lib , "ws2_32.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
hostent* remoteHost;
char* host_name="www.baidu.com";
unsigned int addr;
if (isalpha(host_name[0]))
{ /* host address is a name */
if (host_name[strlen(host_name)-1] == '\n')
host_name[strlen(host_name)-1] = '\0';
remoteHost = gethostbyname(host_name);
}
else
{
addr = inet_addr(host_name);
remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
}
memcpy(&addrSrv.sin_addr.S_un.S_addr,remoteHost->h_addr,remoteHost->h_length);
if (WSAGetLastError() != 0)
{
if (WSAGetLastError() == 11001)
printf("Host not found...\nExiting.\n");
}
else
printf("error#:%ld\n", WSAGetLastError());
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(80);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[1024*200+1]={0};
/////////////////////////////////////////////////////////////////////////////////////////
char sendBuff[200]="GET http://www.baidu.com/s?wd=男人&cl=3 HTTP/1.0\r\nHost:www.baidu.com\r\n\r\n";
send(sockClient,sendBuff,strlen(sendBuff),0);
ofstream outfile("baidu.txt",ios::out);
int cc;
cc=recv(sockClient,recvBuf,1024*200,0);
while(cc!=SOCKET_ERROR&&cc>0)
{
outfile<<recvBuf;
cc=recv(sockClient,recvBuf,1024*200,0);
}
outfile.close();
closesocket(sockClient);
WSACleanup();
}