void SQL_Change(string &str)
{
int len = MultiByteToWideChar(CP_UTF8,0,str.data(),-1,NULL,0);
WCHAR *strNew = new WCHAR[len+1];
MultiByteToWideChar(CP_UTF8,0,str.data(),-1,strNew,len);
str.clear();
str = CW2A(strNew);
delete[] strNew;
return ;
}
// 网络请求
/*
ptszlpDwownURL: 要请求的地址
ptszSendDateail: 要发送的数据,如果为Get请求则置为空
bIsPost: 如果为Post请求则置true,Get置为false
strReturnTexe: 请求返回的文本信息(已转码)
*/
BOOL LK_NetworkNequest(IN TCHAR *ptszlpDwownURL,
IN TCHAR *ptszSendDateail,
IN bool bIsPost,
OUT string &strReturnTexe)
{
DWORD dwBytesRead = 0;
char szRet[MAX_PATH] = { 0 };
HINTERNET hInternetOpen = NULL;
HINTERNET hHttpConnect = NULL;
HINTERNET hHttpRequest = NULL;
PARSEDURLW staUrlInfo;
DWORD dwAccessType = 0; // 网络访问的类型
DWORD dwAccessParameter = 0; // 请求参数
wstring strAccessType; // POST 或者GET
try
{
// 先判断是否联网
if (!::InternetCheckConnection(ptszlpDwownURL, FLAG_ICC_FORCE_CONNECTION, 0))
{
throw 0;
}
// 解析URL以及他的组成部分
TCHAR szHostName[128] = { 0 };
TCHAR szUrlPath[512] = { 0 };
URL_COMPONENTS stUrlAnalysis;
ZeroMemory(&stUrlAnalysis, sizeof(URL_COMPONENTS));
stUrlAnalysis.dwStructSize = sizeof(URL_COMPONENTS);
stUrlAnalysis.dwHostNameLength = sizeof(char) * 128;
stUrlAnalysis.dwUrlPathLength = sizeof(char) * 512;
stUrlAnalysis.lpszHostName = szHostName;
stUrlAnalysis.lpszUrlPath = szUrlPath;
// 解析域名
if (!::InternetCrackUrl(ptszlpDwownURL, 0, ICU_ESCAPE, &stUrlAnalysis))
{
throw 0;
}
// 判断访问类型
if (bIsPost)
strAccessType = _T("POST");
else
strAccessType = _T("GET");
// URL的初步解析
staUrlInfo.cbSize = sizeof(PARSEDURLW);
if (S_OK != ::ParseURLW(ptszlpDwownURL, &staUrlInfo))
{
throw 0;
}
if (URL_SCHEME_HTTPS == staUrlInfo.nScheme)
{
dwAccessType = INTERNET_DEFAULT_HTTPS_PORT;
dwAccessParameter = INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
}
else if (URL_SCHEME_HTTP == staUrlInfo.nScheme)
{
dwAccessType = INTERNET_DEFAULT_HTTP_PORT;
dwAccessParameter = INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE;
}
//初始化WinInet,获取跟句柄
hInternetOpen = ::InternetOpenA(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == hInternetOpen)
{
throw 0;
}
//打开一个HTTP的文件协议
hHttpConnect = ::InternetConnect(hInternetOpen,
szHostName,
dwAccessType,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
1);
if (NULL == hHttpConnect)
{
throw 0;
}
//打开HTTP请求句柄
hHttpRequest = ::HttpOpenRequest(hHttpConnect,
strAccessType.data(),
szUrlPath,
L"HTTP/1.1",
NULL,
0,
dwAccessParameter,
1);
if (NULL == hHttpRequest)
{
throw 0;
}
// 包头 -复杂的包头需要用另外的函数
string strInfoHead;// = "Connection: close";
// 要发送的消息
string strPostNews = CW2A((LPCWSTR)ptszSendDateail);
//向服务器发送这个请求
if (!::HttpSendRequestA(hHttpRequest,
strInfoHead.data(),
strInfoHead.length(),
(void*)strPostNews.data(),
strPostNews.length()))
{
int x = GetLastError();
throw 0;
}
do
{
// 开始下载返回的文本
dwBytesRead = 0;
if (!::InternetReadFile(hHttpRequest, szRet, MAX_PATH - 1, &dwBytesRead))
{
throw 0;
}
szRet[dwBytesRead] = '\0';
strReturnTexe += szRet;
} while (0 < dwBytesRead);
SQL_Change(strReturnTexe);
if (NULL != hInternetOpen)
{
::InternetCloseHandle(hInternetOpen);
}
if (NULL != hHttpConnect)
{
::InternetCloseHandle(hHttpConnect);
}
if (NULL != hHttpRequest)
{
::InternetCloseHandle(hHttpRequest);
}
}
catch (...)
{
if (NULL != hInternetOpen)
{
::InternetCloseHandle(hInternetOpen);
}
if (NULL != hHttpConnect)
{
::InternetCloseHandle(hHttpConnect);
}
if (NULL != hHttpRequest)
{
::InternetCloseHandle(hHttpRequest);
}
return FALSE;
}
return TRUE;
}