//随便用。记得别删这段说明
// -- tchar.h应该是vc微软自己的c sdk。
// -- wininet.h是微软自己的wininet访问库,IE也用
// -- tstrp_URL,要访问的URl格式,必须是 https://domain/abcefgh
// -- tstrp_Retn,可为NULL,即不接受返回信息, dwRetn,可接受的返回数据字节长度
//return 接受到的长度,即需要返回数据时return -1为失败。不然总是return 0
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
extern UINT WINAPI AccessASP(LPTSTR url, LPTSTR t_strRetn= NULL,DWORD dwRetn= 0, bool f_Resend= false);
extern UINT WINAPI AccessHttps (LPTSTR tstrp_URl, LPTSTRtstrp_Retn, DWORD dwRetn);
UINT WINAPI AccessHttps (LPTSTR tstrp_URl, LPTSTR tstrp_Retn, DWORDdwRetn)
{
if (NULL==tstrp_URl)
{
return 0;
}
int i_SecureFlag=INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE|
INTERNET_FLAG_SECURE|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID|INTERNET_FLAG_IGNORE_CERT_CN_INVALID;
LPTSTR tstrp_Object;
LPTSTR tstrp_ServerName= reinterpret_cast<LPTSTR> (malloc ( sizeof(TCHAR)*(lstrlen ( tstrp_URl)+ 1)));
int iPort= INTERNET_DEFAULT_HTTPS_PORT;
if (_tcsstr ( tstrp_URl+ 8">https://*/,":"))
{
iPort= atoi (strstr (tstrp_URl, ":")+ 1);
}
lstrcpy ( tstrp_ServerName, _tcsstr (tstrp_URl, TEXT( "://"))+ 3);
tstrp_Object= _tcsstr ( tstrp_ServerName, TEXT("/"));
if (NULL!= tstrp_Object)
{
*tstrp_Object= ' ';
}
tstrp_Object= _tcsstr ( tstrp_URl, TEXT("://"))+ 3;
if ( NULL!= tstrp_Object)
{
tstrp_Object= _tcsstr (tstrp_Object, TEXT( "/"));
}
int iRetry= 3;
HINTERNET hInternet= InternetOpen( TEXT("TABCGDG"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hSession = InternetConnect(hInternet, tstrp_ServerName, iPort, NULL, NULL,INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET hReq= HttpOpenRequest ( hSession, TEXT("GET"), tstrp_Object, HTTP_VERSION, TEXT ( ""), NULL, i_SecureFlag,NULL);
while ((iRetry--)>0)
{
if ((HttpSendRequest ( hReq,NULL, 0, NULL, 0)))
{
break;
}
if (ERROR_INTERNET_INVALID_CA==GetLastError ( ))
{
DWORDdwFlags;
DWORDdwBuffLen = sizeof(dwFlags);
InternetQueryOption (hReq, INTERNET_OPTION_SECURITY_FLAGS,
(LPVOID)&dwFlags,&dwBuffLen);
dwFlags|= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
dwFlags |=SECURITY_FLAG_IGNORE_WRONG_USAGE;
InternetSetOption (hReq, INTERNET_OPTION_SECURITY_FLAGS,
&dwFlags,sizeof (dwFlags) );
}
}
if (NULL!=tstrp_Retn)
{
InternetReadFile ( hReq,tstrp_Retn, dwRetn, &dwRetn);
}
InternetCloseHandle ( hInternet);
InternetCloseHandle ( hSession);
if ((NULL!=tstrp_Retn)&(0==dwRetn))
{
dwRetn= -1;
}
return dwRetn;
}