⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 amhttpsocket.cpp

📁 一个支持EMPP协议的企信通程序,很完整可以马上用,完全socket底层协议编写,不用移动提供的API
💻 CPP
字号:
#include "StdAfx.h"
#include "AmHttpSocket.h"
#include <atlbase.h>
#include <limits.h>

#define AgentName _T("Nimo Software HTTP Retriever 1.0")

//case insensitive search functions...
#ifdef UNICODE
#define _tcsustr wcsustr
#else
#define _tcsustr strustr
#endif
char* strustr(char *source, char *s);
wchar_t* wcsustr(wchar_t *source, wchar_t *s);

char* strustr(char *source, char *s)
{
	//make an uppercase copy af source and s
	char *csource = strdup(source);
	char *cs = strdup(s);
	strupr(csource);
	strupr(cs);
	//find cs in csource...
	char *result = strstr(csource, cs);
	if (result != NULL)
	{
		//cs is somewhere in csource
		int pos = result - csource;
		result = source;
		result += pos;
	}
	//clean up
	free(csource);
	free(cs);
	return result;
}

wchar_t* wcsustr(wchar_t *source, wchar_t *s)
{
	//make an uppercase copy af source and s
	wchar_t *csource = wcsdup(source);
	wchar_t *cs = wcsdup(s);
	wcsupr(csource);
	wcsupr(cs);
	//find cs in csource...
	wchar_t *result = wcsstr(csource, cs);
	if (result != NULL)
	{
		//cs is somewhere in csource
		int pos = result - csource;
		result = source;
		result += pos;
	}
	//clean up
	free(csource);
	free(cs);
	return result;
}

CAmHttpSocket::CAmHttpSocket()
{
	LastError = 0;
	ReceivedData = NULL;
	Headers = NULL;
	hIO = NULL;
	hIS = NULL;
	hCO = NULL;
	hIO = InternetOpen(AgentName, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
}

CAmHttpSocket::~CAmHttpSocket()
{
	if (ReceivedData != NULL) free(ReceivedData);
	if (Headers != NULL) free(Headers);
	if (hIO != NULL) InternetCloseHandle(hIO);
	if (hIS != NULL) InternetCloseHandle(hIS);
	if (hCO != NULL) InternetCloseHandle(hCO);
}

bool CAmHttpSocket::OpenUrl(const TCHAR *url)
{
	if (hIS != NULL) InternetCloseHandle(hIS);
	hIS = InternetOpenUrl(hIO, url, NULL, 0, HTTP_QUERY_DATE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE, 0);
	if (hIS != NULL) return true;
	else
	{
		LastError = GetLastError();
		return false;
	}
}

bool CAmHttpSocket::PostUrl(const TCHAR *url, const char *PostData, int PostDataLength)
{
	//check length of postdata
	if (PostDataLength == -1)
		PostDataLength = strlen(PostData);
	//some variable that we need...
	URL_COMPONENTS uc;
	//let's split the url...
	uc.dwStructSize = sizeof(uc);
	uc.lpszScheme = NULL;
	uc.dwSchemeLength = 0;
	uc.lpszHostName = NULL;
	uc.dwHostNameLength = 1;
	uc.nPort = 0;
	uc.lpszUserName = NULL;
	uc.dwUserNameLength = 0;
	uc.lpszPassword = NULL;
	uc.dwPasswordLength = 0;
	uc.lpszUrlPath = NULL;
	uc.dwUrlPathLength = 1;
	uc.lpszExtraInfo = NULL;
	uc.dwExtraInfoLength = 0;
	InternetCrackUrl(url, _tcslen(url), 0, &uc);
	//post the data...
	if (hCO != NULL) InternetCloseHandle(hCO);
	TCHAR *HostName = _tcsdup(uc.lpszHostName);
	HostName[uc.dwHostNameLength] = '\0';
	TCHAR *FileName = _tcsdup(uc.lpszUrlPath);
	FileName[uc.dwUrlPathLength] = '\0';
	if (hIS != NULL) InternetCloseHandle(hIS); //if open, close the handle to the connection
	DWORD flags;
	if (uc.nPort == 80)
	{
		//we are talking plain http
		flags = INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_RELOAD;
	}
	else
	{
		//we are talking secure https
		flags = INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_RELOAD;
		//flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_SECURE |
		//	INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID|INTERNET_FLAG_RELOAD;
	}
	TCHAR headers[] = _T("Content-Type: application/x-www-form-urlencoded"); //content type for post...
	//TCHAR headers[] = _T("Content-Type: text/xml"); //content type for post...
	TCHAR szAccept[] = _T("*/*"); //we accept everything...
	LPTSTR AcceptTypes[2]={0};
	AcceptTypes[0]=szAccept;
	hCO = InternetConnect(hIO, HostName, uc.nPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
	hIS = HttpOpenRequest(hCO, _T("POST"), FileName, NULL, NULL, (LPCTSTR*)AcceptTypes, flags, 0);
	if (!HttpSendRequest(hIS, headers, _tcslen(headers), (TCHAR*)PostData, PostDataLength))
	{
		LastError = GetLastError();
		free(HostName);
		free(FileName);
		return false;
	}
	free(HostName);
	free(FileName);
	return true;
}

TCHAR* CAmHttpSocket::GetHeaders(const TCHAR *url)
{
	//did we get an url?
	if (url == NULL)
	{
		LastError = -1;
		return NULL;
	}
	//open the url...
	OpenUrl(url);
	//delete old headers...
	if (Headers != NULL) free(Headers);
	Headers = (TCHAR*)calloc(1, sizeof(TCHAR));
	//get the size headers
	DWORD d = 1, d2 = 0;
	int i = HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2);
	//alloc some space for the headers
	Headers = (TCHAR*)realloc(Headers, d * sizeof(TCHAR));
	if (!HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2)) return NULL;
	return Headers;
}

char* CAmHttpSocket::GetPage(const TCHAR *url, DWORD& size,bool Post, const char *PostData, int PostDataLength)
{
	//did we get an url?
	if (url == NULL)
	{
		LastError = -1;
		return NULL;
	}
	//get the page and store it in ReceivedData...
	if (Post)
	{
		//use http post...
		if (!PostUrl(url, PostData, PostDataLength)) return NULL;
	}
	else
	{
		//use http get
		if (!OpenUrl(url)) return NULL;
	}
	
	if (Headers != NULL) free(Headers);
	Headers = (TCHAR*)calloc(1, sizeof(TCHAR));
	//get the size headers
	DWORD d = 1, d2 = 0;
	int i = HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2);
	//alloc some space for the headers
	Headers = (TCHAR*)realloc(Headers, d * sizeof(TCHAR));
	HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2);
	
	const int rdsize = 8192;
	char mr[rdsize];
	DWORD rd;
	DWORD curpos = 0;
	if (ReceivedData != NULL) 
	{	
		free(ReceivedData);
		ReceivedData=NULL;
	}
	ReceivedData = (char*)calloc(rdsize + 1, sizeof(char));
	while (InternetReadFile(hIS, mr, rdsize - 1, &rd))
	{
		if (rd == 0) break;	
		memcpy(ReceivedData+curpos, mr,rd);
		curpos += rd;		
		ReceivedData = (char*)realloc(ReceivedData, curpos + rdsize);
	}
	size=curpos;
	return ReceivedData;
}

TCHAR* CAmHttpSocket::GetHeaderLine(TCHAR *s)
{
	//find a line in the headers that contains s, and return a pointer to the line...
	if (Headers == NULL) return NULL;
	TCHAR *ts = Headers;
	if (_tcsustr(ts, s) != NULL) return ts;
	while (1)
	{
		if (*ts == '\0' && ts[1] == '\0') break;
		if (*ts == '\0')
		{
			ts++;
			if (_tcsustr(ts, s) != NULL) return ts;
		}
		else ts++;
	}
	return NULL;
}

int CAmHttpSocket::GetPageStatusCode()
{
	//get the correct header line
	TCHAR *s = GetHeaderLine(_T("http"));
	if (s == NULL) return 0; //no headers
	//find the 3 digit code...
	if (_tcslen(s) < 3) return 0; //some error,  the string is too short...
	while (!(isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2])))
	{
		if (s[3] == '\0') return 0; //we have reached the end of the string, without finding the number...
		s++;
	}
	//make a copy of s, and return the code
	TCHAR *code = _tcsdup(s);
	code[3] = '\0'; //remove all text after the 3 digit response code
	int result = _ttoi(code);
	free(code);
	return result;
}

void CAmHttpSocket::FreeBuffer()
{
	if (ReceivedData != NULL) 
	{	
		free(ReceivedData);
		ReceivedData=NULL;
	}
	if (Headers != NULL) 
	{
		free(Headers);
		Headers=NULL;
	}
}

int CAmHttpSocket::CrackURL(const char* Address,char *scheme,
						char *host ,char *path,
						char *extrainfo,int &port)
{
	URL_COMPONENTS url_comp;
	memset(&url_comp,0,sizeof(url_comp));
	url_comp.dwStructSize = sizeof(url_comp);
	url_comp.lpszScheme = scheme;
	url_comp.dwSchemeLength = 100*2;
	url_comp.lpszHostName = host;
	url_comp.dwHostNameLength = 255*2;
	url_comp.lpszUrlPath = path;
	url_comp.dwUrlPathLength = 255*2;
	url_comp.lpszExtraInfo = extrainfo;
	url_comp.dwExtraInfoLength = 255*2;
	BOOL res = InternetCrackUrl((LPCSTR)Address,
		strlen(Address),ICU_ESCAPE,&url_comp);
	port = url_comp.nPort;
	return res;
}

BOOL  CAmHttpSocket::PutFile(const char *szFileName,const char *strSource)
{
	char szScheme[100*2];
	char szHost[255*2];
	char szPath[255*2];
	char szExtrainfo[2*255+1];
	int iPort;		
	char szBuffer[1024*4];	
	ULONG lBufLen = 1024*4;
	DWORD dwIndex = 0;							
	HANDLE hConnect = NULL,hRequest = NULL;		
		
	//初始化		
	//解析出URL地址的各部分
	if(!CrackURL(szFileName,szScheme,szHost,szPath,szExtrainfo,iPort))
	{	
		return FALSE;
	}
	CString strPath = szPath;	
	strPath += szExtrainfo;	
	hConnect  = InternetConnect(hIO,
								(LPCTSTR)szHost,
								iPort,NULL,NULL,
								INTERNET_SERVICE_HTTP,
								0,0);
	if(!hConnect)
	{	
		return FALSE;
	}
	hRequest = HttpOpenRequest (hConnect, "PUT",
         (LPCTSTR)strPath, NULL, NULL, NULL,  0, 0);     
	if(!hRequest)
	{		
		InternetCloseHandle(hConnect);
		return FALSE;
	}		
	DWORD dwFileTotal;
	INTERNET_BUFFERS BufferIn = {0};
	BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );		
	BOOL bRead = 1;	
	DWORD dwBytesRead =0,dwBytesWritten;
	HANDLE hFile = CreateFile (strSource, GENERIC_READ, FILE_SHARE_READ,
         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     if (hFile == INVALID_HANDLE_VALUE)
     {
		InternetCloseHandle(hRequest);	
		InternetCloseHandle(hConnect);			
		return FALSE;
     }
     dwFileTotal=BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);     

     if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
     {
		InternetCloseHandle(hRequest);	
		InternetCloseHandle(hConnect);			
		return FALSE;
     }

     DWORD sum = 0;
	 BOOL bRet;
     while (sum <dwFileTotal)	 
     {
       if  (!(bRead = ReadFile (hFile, szBuffer, lBufLen,
           &dwBytesRead, NULL)))
       {         
		   CloseHandle (hFile);
		   InternetCloseHandle(hRequest);	
		   InternetCloseHandle(hConnect);		
		   return FALSE; 
		}
       if (!(bRet=InternetWriteFile( hRequest, szBuffer, dwBytesRead,
           &dwBytesWritten)))
       {         
		   CloseHandle (hFile);
		   InternetCloseHandle(hRequest);	
		   InternetCloseHandle(hConnect);		
		   return FALSE; 
       }
	
       sum += dwBytesRead;
     }
     
     CloseHandle (hFile);     

     if(!HttpEndRequest(hRequest, NULL, 0, 0))
     {
		InternetCloseHandle(hRequest);	
		InternetCloseHandle(hConnect);			
		return FALSE;       
     }
 	InternetCloseHandle(hRequest);
	InternetCloseHandle(hConnect);	
    return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -