#pragma once
#include
#include "curl\\curl.h"
#include "json\\json.h"
#include
using namespace std;
#pragma comment(lib,"lib_json.lib")
#pragma comment(lib,"LIBCURL.LIB")
enum NetMode
{
NM_UNKNOWN = -1,
NM_POST = 0,
NM_HTTPPOST,
NM_GET,
NM_PUT
};
struct NET_OPT
{
std::string strUrl;
NetMode mode;
std::string file;
std::string key;
std::string text;
};
string UTF8ToGBK(const std::string& strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), strUTF8.length(), NULL, 0);
WCHAR* wszGBK = new WCHAR[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (const char*)strUTF8.c_str(), strUTF8.length(), wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, len, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, len, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
string GBKToUTF8(const std::string& strGBK)
{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), strGBK.length(), NULL, 0);
str1 = new WCHAR[n + 1];
memset(str1, 0, (n + 1) * 2);
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), strGBK.length(), str1, n);
int m = WideCharToMultiByte(CP_UTF8, 0, str1, n, NULL, 0, NULL, NULL);
char * str2 = new char[m + 1];
memset(str2, 0, m + 1);
WideCharToMultiByte(CP_UTF8, 0, str1, n, str2, m, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}
CURL* curl_test_init()
{
CURLcode cd = curl_global_init(CURL_GLOBAL_DEFAULT);
if (cd != CURLE_OK)
return 0;
return curl_easy_init();
}
void curl_test_clean(CURL* curl)
{
if (curl)
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
}
CURLcode curl_test_perform(CURL* curl)
{
return curl_easy_perform(curl);
}
CURLcode curl_test_post(CURL* curl, NET_OPT& no)
{
return CURLE_OK;
}
CURLcode curl_test_httppost(CURL* curl, NET_OPT& nopt)
{
return CURLE_OK;
}
CURLcode curl_test_put(CURL* curl, NET_OPT& nopt)
{
return CURLE_OK;
}
size_t write_get_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
string str;
str.append(ptr, size*nmemb);
string strGBK = UTF8ToGBK(str);
string* p = (string*)userdata;
Json::Value root;
Json::Reader reader;
if (reader.parse(strGBK.c_str(), root))
{
*p = root["text"].asString();
}
return size*nmemb;
}
CURLcode curl_test_get(CURL* curl, NET_OPT& nopt)
{
CURLcode cd = curl_easy_setopt(curl, CURLOPT_URL, nopt.strUrl.c_str());
if (cd != CURLE_OK)
return cd;
cd = curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
if (cd != CURLE_OK)
return cd;
cd = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &nopt.text);
if (cd != CURLE_OK)
return cd;
cd = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_get_callback);
if (cd != CURLE_OK)
return cd;
return CURLE_OK;
}
bool curl_test_setopt(NET_OPT& nopt)
{
CURLcode cd = CURLE_OK;
CURL* curl = curl_test_init();
if (curl == 0)
return false;
if (nopt.mode == NM_GET)
{
cd = curl_test_get(curl, nopt);
}
else if (nopt.mode == NM_HTTPPOST)
{
cd = curl_test_httppost(curl, nopt);
}
else if (nopt.mode == NM_POST)
{
cd = curl_test_post(curl, nopt);
}
else if (nopt.mode == NM_PUT)
{
cd = curl_test_put(curl, nopt);
}
if (cd != CURLE_OK)
return false;
cd = curl_test_perform(curl);
if (cd != CURLE_OK)
return false;
curl_test_clean(curl);
return true;
}
string GetResult(string input)
{
NET_OPT netopt;
string strurl = "http://www.tuling123.com/openapi/api?key=4c4cea3ea907488898463181e3a0fb95&info=";
string inpututf8 = GBKToUTF8(input);
netopt.strUrl = strurl + inpututf8;
netopt.strUrl.append("&userid=[你的id]");
netopt.mode = NM_GET;
if (curl_test_setopt(netopt))
{
return netopt.text;
}
return "";
}
#include
#include
#include
#include
#pragma comment(lib,"winhttp.lib")
#define TULING_URL L"http://www.tuling123.com/openapi/api?key=6c426f89ed3b48cbac84460f60b7635e&info=%s"
static wchar_t String[1024];
//编码转换
char *UnicodeToANSI(const wchar_t *str)
{
static char result[1024];
int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, result, len, NULL, NULL);
result[len] = '\0';
return result;
}
wchar_t *UTF8ToUnicode(const char *str)
{
static wchar_t result[1024];
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, str, -1, result, len);
result[len] = L'\0';
return result;
}
wchar_t *ANSIToUnicode(const char* str)
{
int textlen;
static wchar_t result[1024];
textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
memset(result, 0, sizeof(char) * (textlen + 1));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
return result;
}
bool GetHttpPage(void)
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer = NULL;
static HINTERNET hSession = WinHttpOpen(L"A Tuling API Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
static HINTERNET hConnect = NULL, hRequest = NULL;
BOOL bResults = FALSE;
//从控制台读出一行文字,注意读出来的内容是ANSI编码的,我们需要转换成 Unicode编码
static char uin[1024]; gets_s(uin);
wsprintf(String, TULING_URL, ANSIToUnicode(uin));
//建立一个http的连接会话,给出主机名就行,可以域名,也可以是IP地址,不需要http;前缀
if (hSession)
{
if (!hConnect)
{
hConnect = WinHttpConnect(hSession, L"www.tuling123.com", INTERNET_DEFAULT_HTTP_PORT, 0);
}
}
//创建一个HTTP请求句柄
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"POST", String, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_ESCAPE_PERCENT | WINHTTP_FLAG_REFRESH);
//发送请求数据
if (hRequest)
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
// 请求结束,接收数据
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
else
printf("Error %d has occurred.\n", GetLastError());
//如果返回值为false,可以使用getlasterror来得到错误信息,下同
//返回值的详细信息可以看微软网页,或者看这里翻译好的中文接口说明
//http://blog.csdn.net/fengsh998/article/details/8201591
// 内部使用的一个循环来确保能接受到所有数据
if (bResults)
{
do
{
//检查是否还有数据需要接收
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
break;
}
if (!dwSize)
break;
//为缓冲分配内存并读取
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
break;
}
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
printf("Error %u in WinHttpReadData.\n", GetLastError());
}
else
{
//图灵api返回来的内容使用的是UTF-8编码,我们需要把它转换回ANSI才能在控制台显示
//printf("return:%s\n", UnicodeToANSI(UTF8ToUnicode(pszOutBuffer)) );
//因为没有使用JSON库,所以我暴力拆了这字符串。
pszOutBuffer[strlen(pszOutBuffer) - 2] = '\0';
printf("小灵:%s\n\n", UnicodeToANSI(UTF8ToUnicode(pszOutBuffer)) + 23);
return true;
}
delete[] pszOutBuffer;
if (!dwDownloaded)
break;
} while (dwSize > 0);
}
//收尾,关闭被打开的句柄
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return false;
}
int main(void)
{
do { printf("我:"); } while (GetHttpPage());
system("pause");
return 0;
}