<code class="lang-cpp">CString mbcs2tcs(CStringA astr, bool isutf8) { if (isutf8) { int length = MultiByteToWideChar(CP_UTF8, 0, astr, -1, NULL, 0); wchar_t *bufnew = new wchar_t[length + 1]; MultiByteToWideChar(CP_UTF8, 0, astr, -1, bufnew, length); bufnew[length] = 0; CString wstr(bufnew); delete []bufnew; return wstr; } else { return (CString) astr; } } CStringA tcs2mbcs(CString str, bool isutf8) { if (isutf8) { CStringW wstr = (CStringW) str; int length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); char *bufnew = new char[length + 1]; WideCharToMultiByte(CP_UTF8, 0, wstr, -1, bufnew, length, NULL, NULL); bufnew[length] = 0; CStringA astr(bufnew); delete []bufnew; return astr; } else { return (CStringA) str; } }</code>
<code class="lang-cpp">void CHttpTestDlg::OnBnClickedRunRequest() { UpdateData(true); try { CInternetSession inetsess; // CInternetSession CHttpFile *file = NULL; file = (CHttpFile*) inetsess.OpenURL(m_url, 1, INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_RELOAD, m_reqheader, _tcslen(m_reqheader)); m_recv = _T(""); // 清空文本框 char buf[1024] = {0}; int ncount; CStringA ansistr; while (ncount = file->Read(buf, 1023)) { buf[ncount] = '\0'; ansistr += buf; } m_recv = mbcs2tcs(ansistr, m_isutf8); // 从GB2312转码到wchar_t(如果是ANSI编译则不转码)输出到文本框 file->Close(); // 关闭请求文件 if (file != NULL) delete file; inetsess.Close(); // 关闭CInternetSession } catch (CException &e) { m_recv = _T("获取错误"); } UpdateData(false); }</code>运行截图:
<code class="lang-cpp">void CHttpTestDlg::OnBnClickedPostCmd() { UpdateData(true); try { CInternetSession inetsess; // CInternetSession CHttpConnection *connect = NULL; CHttpFile *file = NULL; { // POST请求:通过CInternetSession打开HTTP连接,然后再用HTTP连接打开HTTP请求,发送请求 DWORD svctype; CString srvname; CString objname; INTERNET_PORT port; AfxParseURL(m_url, svctype, srvname, objname, port); // 解析URL,得到服务器名、路径名、端口名等 connect = inetsess.GetHttpConnection(srvname, port); // 建立HTTP连接,到服务器和端口 file = connect->OpenRequest(_T("POST"), objname); // 打开HTTP请求,类型POST,到路径名 // 发送请求,附带头部和附加信息。附加数据必须为ANSI或UTF-8,要进行转换 CStringA postdata = tcs2mbcs(m_postdata, m_isutf8); LPCSTR postbuffer = (LPCSTR)postdata; file->SendRequest(m_reqheader, m_reqheader.GetLength(), (LPVOID)postbuffer, strlen(postbuffer)); } m_recv = _T(""); // 清空文本框 char buf[1024] = {0}; int ncount; CStringA ansistr; while (ncount = file->Read(buf, 1023)) { buf[ncount] = '\0'; ansistr += buf; } m_recv = mbcs2tcs(ansistr, m_isutf8); // 从GB2312转码到wchar_t(如果是ANSI编译则不转码)输出到文本框 file->Close(); connect->Close(); if (file != NULL) // 关闭请求文件 delete file; if (connect != NULL) // 关闭连接 delete connect; inetsess.Close(); // 关闭CInternetSession } catch (CException &e) { m_recv = _T("获取错误"); } UpdateData(false); }</code>运行截图:
[修改于 9年7个月前 - 2015/06/12 09:50:36]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |