www.pudn.com > thread_insert.rar > WEBClient.cpp


// WEBClient.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include "winsock.h" 
#include "stdio.h" 
#pragma comment(lib,"ws2_32.lib") 
#define winsock_version 0x0101 
inline unsigned indexOf(char * strlong, char * str2find); 
bool getsession(char * session,char * streply); 
void main() 
{	char host_name[50]; 
    SOCKADDR_IN saServer; 
	LPHOSTENT lphostent; 
	WSADATA wsadata; 
    SOCKET hsocket; 
	char what[20]; 
	char end[1000]; 
	int nRet; 
    char hostname[1024] ; 
	char who[20]; 
 
	printf("type which server you want to connect:"); 
	scanf("%s",who); 
start: 
	printf("type which file you want or exit to exit:"); 
	scanf("%s",what); 
	if(!strcmp(what,"exit")) goto out; 
	wsprintf(hostname,"GET ");///index.jsp HTTP/1.0 %c%c",10,10); 
	strcat(hostname,what); 
	//wsprintf(end," HTTP/1.0 %c%c",10,10);Cookie: username=GLKDBY00584; password=501;  
	//wsprintf(end," HTTP/1.0 %c%c%c%c",10,10,10,10); 
	//wsprintf(end," HTTP/1.1\r\n\r\n"); 
	wsprintf(end," HTTP/1.1\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\nAccept-Language: zh-cn\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\r\nHost: 61.235.170.28\r\nConnection: Keep-Alive\r\nCookie: itellin=1; operatingSystem=1; username=GLKDBY00584; password=5014; saveflag=1; JSESSIONID=aaa-d2rvPLHy7d\r\n\r\n"); 
	strcat(hostname,end); 
	char dest[2048]; 
	if(WSAStartup(winsock_version,&wsadata)) 
		printf("can't open"); 
	sprintf(host_name,who); 
    lphostent=gethostbyname(who); 
    if(lphostent==NULL)		printf("lphostent is null"); 
	hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    saServer.sin_family = AF_INET; 
	// Use def. now, need to handle general case 
	saServer.sin_port = htons(443);	 
	saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); 
    nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN)); 
	if (nRet == SOCKET_ERROR) 
	{ 
		printf("can't connect"); 
		closesocket(hsocket); 
		return; 
	} 
	else 
		printf("connected with %s\n",host_name); 
	printf("send %s to server\n",hostname); 
    nRet = send(hsocket, hostname, strlen(hostname), 0); 
	if (nRet == SOCKET_ERROR) 
	{ 
		printf("send() failed"); 
		closesocket(hsocket);		 
	} 
	else 
		printf("send() OK\n"); 
	nRet=1; 
	while(nRet>0) 
	{ 
		nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0); 
		if(nRet>0) 
			dest[nRet]=0; 
		else 
			dest[0]=0; 
		printf("\nReceived bytes:%d\n",nRet); 
		printf("Result:\n%s",dest); 
	} 
	printf("\t\t\trecv %d times:\n",nRet); 
	printf("\t\tget session id..."); 
	char sessionid[100]; 
	if(!getsession(sessionid,dest))  
	{ 
		printf("\t\tget session failed!\n"); 
		goto start; 
	} 
	printf("find a session:%s\n",sessionid);   memset(hostname,0,sizeof(hostname)); 
	wsprintf(hostname,"GET /index.jsp HTTP/1.0 %c%cCookie: itellin=1; operatingSystem=1; username=GLKDBY00584; password=501;saveflag=1; ",10,10); 
	strcat(hostname,sessionid);  
	char path[10]; 
	wsprintf(path,"; Path=/%c%c",10,10); 
	strcat(hostname,path); 
	printf("%s\n",hostname); 
	if(WSAStartup(winsock_version,&wsadata)) 
		printf("can't open"); 
	sprintf(host_name,who); 
    lphostent=gethostbyname(who); 
    if(lphostent==NULL)		printf("lphostent is null"); 
	hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    saServer.sin_family = AF_INET; 
	// Use def. now, need to handle general case 
	saServer.sin_port = htons(80);	 
	saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); 
	nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN)); 
	if (nRet == SOCKET_ERROR) 
	{ 
		printf("can't connect\n"); 
		closesocket(hsocket); 
		goto start; 
	} 
	else 
		printf("connected with %s\n",host_name); 
	 nRet = send(hsocket, hostname, strlen(hostname), 0); 
	if (nRet == SOCKET_ERROR) 
	{ 
		printf("send() failed"); 
		closesocket(hsocket);		 
	} 
	else 
		printf("send() OK\n"); 
	nRet=1; 
	while(nRet>0) 
	{ 
		nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0); 
		if(nRet>0) 
			dest[nRet]=0; 
		else 
			dest[0]=0; 
		printf("\nReceived bytes:%d\n",nRet); 
		printf("Result:\n%s",dest); 
	} 
	printf("recv %d times:\n",nRet); 
	getchar(); 
	goto start; 
out: 
	closesocket(hsocket); 
	WSACleanup(); 
} 
//////////////////////////////////////////////////////// 
inline unsigned indexOf(char * strlong, char * str2find) 
{	bool flag=true; 
	//printf("hello;%s:%d\n",strlong,strlen(strlong)); 
	//printf("string:%s\n",str2find,strlen(str2find)); 
	//printf("strlen(strlong)-strlen(str2find):%d\n",strlen(strlong)-strlen(str2find)); 
	for(unsigned i=0;(i<( strlen(strlong)-strlen(str2find)))&&(strlong[i]!='\0');i++) 
	{	flag=true; 
		for(unsigned point=0;point