图灵机器人API调用 C++版

这是一个非常简单的例子,作为新手的我是拿来练手的,当然也可以给和我一样的朋友一些参考。

而且图灵官网没有给出C的例子,网上一搜也是各种Java、C#甚至易语言实现,不要歧视C++好不好●︿●,就算不如语言老大PHP,它也是很强的!

 

这个例子其本质就是一个C++写的get数据  (POST和这个也差不多啦,可以自己动手试一试╭(′▽`)╯ )

没有用MFC,直接用的WindowAPI哦,用的是winhttp。

也没有使用JSON库之类的来解析数据,因为我是暴力拆解字符串的,所以如果返回值位数不对可能会乱码(大雾)。

好了,放源码:

 

  1 /*
  2 图灵机器人 C++实现
  3 
  4 极简版
  5 几乎没有界面(废话,毕竟是控制台)
  6 
  7 代码参考自微软提供的例子,见:
  8 https://msdn.microsoft.com/en-us/library/windows/desktop/aa384104(v=vs.85).aspx
  9 */
 10 
 11 #include <iostream>
 12 #include <Windows.h> 
 13 #include <winhttp.h> 
 14 #pragma comment(lib,"winhttp.lib")
 15 
 16 #define TULING_URL  L"www.tuling123.com/openapi/api?key=这里换成你自己从图灵申请的API啦&info=%s"
 17 static wchar_t String[1024];
 18 
 19 //编码转换
 20 char *UnicodeToANSI(const wchar_t *str)
 21 {
 22     static char result[1024];
 23     int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
 24     WideCharToMultiByte(CP_ACP, 0, str, -1, result, len, NULL, NULL);
 25     result[len] = '\0';
 26     return result;
 27 }
 28 wchar_t *UTF8ToUnicode(const char *str)
 29 {
 30     static wchar_t result[1024];
 31     int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
 32     MultiByteToWideChar(CP_UTF8, 0, str, -1, result, len);
 33     result[len] = L'\0';
 34     return result;
 35 }
 36 wchar_t *ANSIToUnicode(const char* str)
 37 {
 38     int textlen;
 39     static wchar_t result[1024];
 40     textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
 41     memset(result, 0, sizeof(char) * (textlen + 1));
 42     MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
 43     return result;
 44 }
 45 
 46 bool GetHttpPage(void)
 47 {
 48     DWORD dwSize = 0;
 49     DWORD dwDownloaded = 0;
 50     LPSTR pszOutBuffer = NULL;
 51     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);
 52     static HINTERNET hConnect = NULL, hRequest = NULL;
 53     BOOL  bResults = FALSE;
 54 
 55     //从控制台读出一行文字,注意读出来的内容是ANSI编码的,我们需要转换成 Unicode编码
 56     static char uin[1024]; gets_s(uin);
 57     wsprintf(String, TULING_URL, ANSIToUnicode(uin));
 58 
 59     //建立一个http的连接会话,给出主机名就行,可以域名,也可以是IP地址,不需要http;前缀
 60     if (hSession)
 61     {
 62         if (!hConnect)
 63             hConnect = WinHttpConnect(hSession, L"www.tuling123.com", INTERNET_DEFAULT_HTTP_PORT, 0);
 64     }
 65 
 66     //创建一个HTTP请求句柄
 67     if (hConnect)
 68         hRequest = WinHttpOpenRequest(hConnect, L"GET", String, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_ESCAPE_PERCENT | WINHTTP_FLAG_REFRESH);
 69 
 70     //发送请求数据
 71     if (hRequest)
 72         bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
 73 
 74     // 请求结束,接收数据
 75     if (bResults)
 76         bResults = WinHttpReceiveResponse(hRequest, NULL);
 77     else
 78         printf("Error %d has occurred.\n", GetLastError());
 79     //如果返回值为false,可以使用getlasterror来得到错误信息,下同
 80     //返回值的详细信息可以看微软网页,或者看这里翻译好的中文接口说明
 81     //http://blog.csdn.net/fengsh998/article/details/8201591
 82 
 83     // 内部使用的一个循环来确保能接受到所有数据
 84     if (bResults)
 85     {
 86         do
 87         {
 88             //检查是否还有数据需要接收
 89             dwSize = 0;
 90             if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
 91             {
 92                 printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
 93                 break;
 94             }
 95 
 96             if (!dwSize)
 97                 break;
 98 
 99             //为缓冲分配内存并读取
100             pszOutBuffer = new char[dwSize + 1];
101 
102             if (!pszOutBuffer)
103             {
104                 printf("Out of memory\n");
105                 break;
106             }
107 
108             ZeroMemory(pszOutBuffer, dwSize + 1);
109 
110             if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
111             {
112                 printf("Error %u in WinHttpReadData.\n", GetLastError());
113             }
114             else
115             {
116                 //图灵api返回来的内容使用的是UTF-8编码,我们需要把它转换回ANSI才能在控制台显示
117                 //printf("return:%s\n", UnicodeToANSI(UTF8ToUnicode(pszOutBuffer)) );
118 
119                 //因为没有使用JSON库,所以我暴力拆了这字符串。
120                 pszOutBuffer[strlen(pszOutBuffer)-2] = '\0';
121                 printf("小灵:%s\n\n", UnicodeToANSI(UTF8ToUnicode(pszOutBuffer)) + 23);
122                 return true;
123             }
124 
125             delete[] pszOutBuffer; 
126             if (!dwDownloaded)
127                 break;
128 
129         } while (dwSize > 0);
130     }
131     
132     //收尾,关闭被打开的句柄
133     if (hRequest) WinHttpCloseHandle(hRequest);
134     if (hConnect) WinHttpCloseHandle(hConnect);
135     if (hSession) WinHttpCloseHandle(hSession);
136 
137     return false;
138 }
139 
140 int main(void)
141 {
142     system("color F0");
143     system("title 会聊天的图灵机器人 ●﹏●");
144     printf("\n  我是小灵,快来和我聊天吧! ●▽●\n\n");
145 
146     do{ printf("我:"); } while (GetHttpPage());
147 
148     system("pause");
149     return 0;
150 }

 

VS2013可以直接Paste后编译就用,额需要注意的是使用 Unicode 字符集,如果不是的话可能会出现不可预料的东西(因为我没试过╮(╯▽╰)╭)

所有代码包括注释加起来是150行,编译后也才10K左右,可以放心食用!

哦,再放张图片好了

posted @ 2015-12-13 19:48  _st  阅读(3800)  评论(0编辑  收藏  举报