#include <iostream>
#include <string>
#include <windows.h>
#include <wininet.h>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "Wininet.lib")
#define MAXBLOCKSIZE 1024
bool downLoadFile(const char *Url, const TCHAR *ptFilePath)
{
bool bret = false;
HINTERNET hSession = InternetOpenA("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrlA(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
//printf("%s\n",Url);
byte Temp[MAXBLOCKSIZE] = {0};
ULONG Number = 1;
DWORD dwBytesWritten ;
HANDLE hFile;
// Open the file, creating it if necessary
if (INVALID_HANDLE_VALUE !=
(hFile = CreateFile (ptFilePath, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL)))
{
while (Number > 0)
{
InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
WriteFile (hFile, Temp, Number * sizeof (byte),
&dwBytesWritten, NULL);
}
CloseHandle(hFile);
}
InternetCloseHandle(handle2);
handle2 = NULL;
bret = true;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
return bret;
}
std::string WcsToUtf8(const std::wstring &wcs)
{
char* pElementText;
int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wcs.c_str(), -1, NULL, 0, NULL, NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wcs.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strReturn(pElementText);
delete[] pElementText;
return strReturn;
}
std::string GBToUTF8(const std::string &strgb2312)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strgb2312.c_str(), -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len];
//memset(wszUtf8, 0, len * 2 );
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strgb2312.c_str(), -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len];
//memset(szUtf8, 0, len);
WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);
std::string strutf8 = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
return strutf8;
}
// Unicode 转换成UTF-8
void UnicodeToUTF_8(char* pOut,WCHAR* pText)
{
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
unsigned char ToHex(unsigned char x)
{
return x > 9 ? x + 55 : x + 48;
}
std::string UrlEncode(const std::string& str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (isalnum((unsigned char)str[i]) ||
(str[i] == '-') ||
(str[i] == '_') ||
(str[i] == '.') ||
(str[i] == '~'))
strTemp += str[i];
else if (str[i] == ' ')
strTemp += "+";
else
{
strTemp += '%';
strTemp += ToHex((unsigned char)str[i] >> 4);
strTemp += ToHex((unsigned char)str[i] % 16);
}
}
return strTemp;
}
int main()
{
TCHAR ptExePath[MAX_PATH] = {0};
GetModuleFileName(NULL, ptExePath, MAX_PATH);
PathRemoveFileSpec(ptExePath);
lstrcat(ptExePath, TEXT("\\"));
lstrcat(ptExePath, TEXT("春思.mp3"));
std::string strurl("http://tsn.baidu.com//text2audio?tex=");
std::string strtext = GBToUTF8("燕草如碧丝,秦桑低绿枝.当君怀归日,是妾断肠时,春风不相识,何事入罗帏?");
strtext = UrlEncode(strtext);
strurl += strtext;
strurl += "&lan=zh&tok=24.286d71cb5509b0498221409b4c36465d.2592000.1459400760.282335-7808972&ctp=1&cuid=7808972";
DWORD dwStart = GetTickCount();
downLoadFile(strurl.c_str(), ptExePath);
DWORD dwEnd = GetTickCount();
printf("耗时:%d毫秒\n", dwEnd - dwStart);
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
前往页