#include "stdafx.h"
#include "afxinet.h"
#include "HttpClient.h"
#pragma comment(lib,"ws2.lib")
#define BUFFER_SIZE 1024
CHttpClient::CHttpClient(void)
{
m_connect = FALSE;
m_send = FALSE;
m_recv =FALSE;
m_File = TRUE;
retcode = 0;
}
CHttpClient::~CHttpClient(void)
{
}
void CHttpClient::addParam(CString strName,CString strValue )
{
m_names.AddTail((LPCTSTR)strName);
m_values.AddTail((LPCTSTR)strValue);
CString eq=_T("=");
CString an=_T("&");
m_strContent=m_strContent+strName+eq+strValue;
m_strCL=m_strContent.GetLength();
}
CString CHttpClient::doPost(CString strHref,CString m_Content )
{
m_url=strHref;
m_strContent = m_Content;
m_thread = CreateThread(NULL,0,doPostThread,this,0,NULL);
DWORD dwRet = WaitForSingleObject(m_thread,4000);
TerminateThread(m_thread,0);
CloseHandle(m_thread);
if(dwRet==WAIT_TIMEOUT)
{
if (m_connect&&m_send&&m_recv)
{
return cs;
}
else if (m_connect&&m_send)
{
return _T("-1");
}
else if (m_connect)
{
return _T("-2");
}
else
{
return _T("-3");
}
}
return cs;
}
CString CHttpClient::doPost( CString strHref )
{
CString strHttpsource;
CInternetSession session;
CHttpConnection* pConnect=NULL;
CHttpFile* pFile = NULL;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
CString cs;
AfxParseURL((LPCTSTR)strHref,dwServiceType, strServerName, strObject, nPort);
USES_CONVERSION;
try
{
pConnect = session.GetHttpConnection(strServerName,nPort);
pFile = pConnect->OpenRequest(0,strObject,NULL,1,NULL,_T("HTTP/1.1"),INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile -> AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
pFile -> AddRequestHeaders(_T("Accept: */*"));
pFile -> SendRequest(NULL,0,NULL,0);
pFile -> QueryInfoStatusCode(retcode);
}
catch (CInternetException * e)
{
pFile = NULL;
pConnect = NULL;
};
if(pFile)
{
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
cs=pUnicode;
delete[] pUnicode;
pUnicode =NULL;
}
if(pFile)
{
delete pFile;
pFile = NULL;
}
if(pConnect)
{
delete pConnect;
pConnect = NULL;
}
session.Close();
return strHttpsource;
}
CString CHttpClient::doGet(CString strHref)
{
CString strHttpsource;
CInternetSession session(NULL,0);
CString cs;
CHttpFile* pHTTPFile = NULL;
try
{
pHTTPFile = (CHttpFile*)session.OpenURL(strHref);
}
catch(CInternetException)
{
pHTTPFile = NULL;
}
if(pHTTPFile)
{
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = pHTTPFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
cs=pUnicode;
delete[] pUnicode;
pUnicode =NULL;
}
else
{
}
return strHttpsource;
}
DWORD _stdcall CHttpClient::doPostThread( LPVOID lpData )
{
CHttpClient* pThis = (CHttpClient*)lpData;
pThis->_doPostThread();
return 0;
}
void CHttpClient::_doPostThread()
{
CString strHttpsource;
CInternetSession session;
CHttpConnection* pConnect=NULL;
CHttpFile* pFile = NULL;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
AfxParseURL((LPCTSTR)m_url,dwServiceType, strServerName, strObject, nPort);
TCHAR* outBuff = m_strContent.GetBuffer();
// INTERNET_PROXY_INFO proxyinfo;
// proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
// proxyinfo.lpszProxy =_T("211.104.243.73:8080");
// proxyinfo.lpszProxyBypass = NULL;
// session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,
// sizeof(INTERNET_PROXY_INFO));
// session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000); // 5秒的连接超时
// session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); // 1秒的发送超时
// session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000); // 7秒的接收超时
// session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000); // 1秒的发送超时
// session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 1000); // 1秒的接收超时
USES_CONVERSION;
try
{
pConnect = session.GetHttpConnection(strServerName,nPort);
if (pConnect!=NULL)
{
m_connect = TRUE;
}
pFile = pConnect->OpenRequest(0,strObject,NULL,1,NULL,_T("HTTP/1.1"),INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile -> AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
pFile -> AddRequestHeaders(_T("Accept: */*"));
pFile -> SendRequest(NULL,0,T2A(outBuff),_tcslen(outBuff));
pFile -> QueryInfoStatusCode(retcode);
if (retcode==201||retcode ==202)
{
m_send = TRUE;
}
else if (retcode == 200)
{
m_send = TRUE;
m_recv = TRUE;
}
}
catch (CInternetException * e)
{
pFile = NULL;
pConnect = NULL;
m_File = FALSE;
};
if(pFile&&m_File)
{
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
cs=pUnicode;
delete[] pUnicode;
pUnicode =NULL;
}
if(pFile)
{
delete pFile;
pFile = NULL;
}
if(pConnect)
{
delete pConnect;
pConnect = NULL;
}
session.Close();
}
int CHttpClient::doPost(char* ip,int port,char* hostname, char* url, char* parameters,CString &ret,HttpType type)
{
WSADATA WsaData;
if (WSAStartup(MAKEWORD(2,2), &WsaData)==-1)
{
return -100;
}
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short)port);
sin.sin_addr.s_addr = inet_addr(ip);
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock<=0)
{
return -101;
}
timeval tm;
fd_set set;
unsigned long ul = 1;
bool retbool = false;
int error = -1;
ioctlsocket(sock, FIONBIO, &ul); //设置为非阻塞模式
//建立连接
if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in) )<0)
{
tm.tv_sec = 2; //设置两秒连接超时 (send超时)
tm.tv_usec = 0;
FD_ZERO(&set);
FD_SET(sock, &set);
if(select(sock+1, NULL, &set, NULL, &tm) > 0)
{
if(FD_ISSET(s