MFC HTTP 上传下载
2016-05-30 17:00
519 查看
比较简单的是文件下载,这个很简单,所以直接把程序贴出来
1--添加头文件和lib
2--文件下载:
3--文件上传
要注意,文件上传的调用格式,另外服务器那边也要有一个php文件来接收上传的文件,因为我是基于php的平台,
而且只能上传小于1M的文件
服务端的脚本:
调用程序:
//测试连接:
//附加:删除文件
1--添加头文件和lib
#include <afxinet.h> #include "afxwin.h" #pragma comment(lib,"Wininet.lib") #define __BUFFER_SIZE 1024 |
2--文件下载:
BOOL CHTCodeServerSYSDlg::DownLoadFile(LPCTSTR szRemoteURI, LPCTSTR szLocalPath) { ASSERT(NULL != szRemoteURI && NULL != szLocalPath); CInternetSession session; CHttpConnection* pHttpConnection = NULL; CHttpFile* pHttpFile = NULL; CString strServer = _T(""), strObject = _T(""); INTERNET_PORT wPort = 0; DWORD dwType = 0; DWORD dwFlag = 0; HANDLE hFile = NULL; TCHAR pszBuffer[__BUFFER_SIZE]; DWORD dwFileSize = 0; const int nTimeOut = 2000; session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); BOOL bResult = FALSE; try { //用二进制形势传输,不使用缓存,强制重载数据 //如果没有设置这个标志那么每次从服务器下载的文件会都一样,就算服务器上的文件变了,也还一样 //原因就是缓存问题,所以设置这个标志就能防止那种坑爹问题 dwFlag = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD; AfxParseURL(szRemoteURI, dwType, strServer, strObject, wPort); pHttpConnection = session.GetHttpConnection(strServer, wPort, NULL, NULL); ASSERT(NULL != pHttpConnection); pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwFlag); ASSERT(NULL != pHttpFile); if (!pHttpFile->SendRequest()) goto _err_handler; //INTERNET_FLAG_RELOAD DWORD dwStateCode; pHttpFile->QueryInfoStatusCode(dwStateCode); if (dwStateCode != HTTP_STATUS_OK) goto _err_handler; hFile = CreateFile(szLocalPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { DWORD reterr = GetLastError(); goto _err_handler; } BOOL bRet = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dwFileSize); if (!bRet) goto _err_handler; DWORD dwWrite = 0, dwTotalWrite = 0; UINT nRead = 0; do { nRead = pHttpFile->Read(pszBuffer, __BUFFER_SIZE); if (nRead <= 0) break; WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); ASSERT(nRead == dwWrite); dwTotalWrite += dwWrite; } while (TRUE); if (dwTotalWrite != dwFileSize) goto _err_handler; bResult = TRUE; } catch (CFileException* e) { e->Delete(); //REPORT_CACHED_EXCEPTION(_T("CFileException")); } catch (CInternetException* e) { CString sError; sError.Format(_T("Inernet connection error : %d"), e->m_dwError); e->Delete(); //REPORT_CACHED_EXCEPTION(sError); } _err_handler: if (pHttpFile) { pHttpFile->Close(); delete pHttpFile; } if (pHttpConnection) { pHttpConnection->Close(); delete pHttpConnection; } session.Close(); if (hFile) CloseHandle(hFile); return bResult; } |
3--文件上传
要注意,文件上传的调用格式,另外服务器那边也要有一个php文件来接收上传的文件,因为我是基于php的平台,
而且只能上传小于1M的文件
BOOL CHTScannerMenageSYSDlg::UploadFile(LPCTSTR szRemoteURI, LPCTSTR szLocalPath) { ASSERT(NULL != szRemoteURI && NULL != szLocalPath); BOOL bResult = FALSE; DWORD dwType = 0; CString sServer = _T(""); CString sObject = _T(""); INTERNET_PORT wPort = 0; DWORD dwPostSize = 0; const int nTimeOut = 5000; CHttpConnection* pHttpConn = NULL; CHttpFile* pHttpFile = NULL; CInternetSession cis(_T("MYyyh")); INTERNET_BUFFERS BufferIn; DWORD dwTotalWritten = 0; BYTE pFileBuffer[__BUFFER_SIZE]; //DWORD dwPostSize = 0; CFile file; BOOL bRet = FALSE; bResult = AfxParseURL(szRemoteURI, dwType, sServer, sObject, wPort); if (!bResult) return FALSE; bResult = FALSE; try { cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); pHttpConn = cis.GetHttpConnection(sServer, wPort, NULL, NULL); ASSERT(NULL != pHttpConn); pHttpFile = pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_POST, sObject); ASSERT(NULL != pHttpFile); bRet = file.Open(szLocalPath, CFile::shareDenyNone | CFile::modeRead); if (!bRet) return FALSE; dwPostSize = (DWORD)file.GetLength(); ASSERT(dwPostSize >= 0 && dwPostSize < 0x80000000); memset(&BufferIn, 0, sizeof(BufferIn)); BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = NULL; BufferIn.dwHeadersLength = 0; BufferIn.dwHeadersTotal = 0; BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; if (!pHttpFile->SendRequestEx(&BufferIn, NULL, HSR_INITIATE, 1)) { TRACE1("Error on HttpSendRequestEx %d\n", GetLastError()); return FALSE; } file.SeekToBegin(); do { int nActual = file.Read(pFileBuffer, __BUFFER_SIZE); if (nActual <= 0) break; pHttpFile->Write(pFileBuffer, nActual); dwTotalWritten += nActual; } while (TRUE); if (dwTotalWritten != dwPostSize) { file.Close(); TRACE1("\nError on InternetWriteFile %lu \n", GetLastError()); return FALSE; } if (!pHttpFile->EndRequest(0, NULL, 1)) { file.Close(); TRACE1("Error on HttpEndRequest %lu \n", GetLastError()); return FALSE; } file.Close(); DWORD dwStateCode = 0; pHttpFile->QueryInfoStatusCode(dwStateCode); if (dwStateCode == HTTP_STATUS_CREATED || dwStateCode == HTTP_STATUS_OK) bResult = TRUE; } catch (CFileException* e) { e->Delete(); //REPORT_CACHED_EXCEPTION(_T("CFileException")); } catch (CInternetException* e) { e->Delete(); CString sError; sError.Format(_T("Inernet connection error : %d"), e->m_dwError); //REPORT_CACHED_EXCEPTION(sError); } if (pHttpFile) { pHttpFile->Close(); delete pHttpFile; } if (pHttpConn) { pHttpConn->Close(); delete pHttpConn; } cis.Close(); return bResult; } |
<!DOCTYPE html> <html> <body> <?php $myfile = "dfsgetco.dll"; //php://input表示获取传上来的数据,不过有大小限制,似乎是1M以下 $post = file_get_contents("php://input"); file_put_contents( $myfile, $post); ?> </body> </html> |
调用程序:
//上传 void CHTScannerMenageSYSDlg::OnBnClickedButton20() { // TODO: 在此添加控件通知处理程序代码 //upget.php服务器上要处理数据的脚本的名称 CString szUrl = TEXT("你服务器的地址/w3ctest/upget.php"); UploadFile(szUrl, _T("D:\\Topicddd.dll")); } //下载 void CHTScannerMenageSYSDlg::OnBnClickedButton21() { // TODO: 在此添加控件通知处理程序代码 CString szUrl = TEXT("服务器的地址/w3ctest/httptest/Topicx.dll"); DownLoadFile(szUrl, _T("D:\\Topicx.ini")); } |
BOOL CHTScannerMenageSYSDlg::CanWebsiteVisit(CString sURI) { CHttpConnection* pHttpConn = NULL; CHttpFile* pHttpFile = NULL; CInternetSession cis; BOOL bResult = FALSE; BOOL bRetVal = FALSE; DWORD dwType = 0; DWORD dwStateCode = 0; INTERNET_PORT wPort = 0; CString sServer = _T(""); CString sObject = _T(""); const int nTimeOut = 5000; try { bResult = AfxParseURL(sURI, dwType, sServer, sObject, wPort); if (!bResult) return FALSE; cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); pHttpConn = cis.GetHttpConnection(sServer, wPort); if (pHttpConn) { pHttpFile = pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_GET, sObject); if (pHttpFile->SendRequest()) { pHttpFile->QueryInfoStatusCode(dwStateCode); if (dwStateCode == HTTP_STATUS_CREATED || dwStateCode == HTTP_STATUS_OK) bRetVal = TRUE; } } } catch (CInternetException* e) { e->Delete(); } catch (...) { } if (pHttpFile) { pHttpFile->Close(); delete pHttpFile; } if (pHttpConn) { pHttpConn->Close(); delete pHttpConn; } cis.Close(); return bRetVal; } |
//附加:删除文件
BOOL CHTScannerMenageSYSDlg::DeleteFile(LPCTSTR szRemoteURI) { ASSERT(NULL != szRemoteURI); CInternetSession session; CHttpConnection* pHttpConnection = NULL; CHttpFile* pHttpFile = NULL; CString strServer = _T(""), strObject = _T(""); INTERNET_PORT wPort = 0; DWORD dwType = 0; const int nTimeOut = 2000; session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); BOOL bResult = FALSE; try { AfxParseURL(szRemoteURI, dwType, strServer, strObject, wPort); pHttpConnection = session.GetHttpConnection(strServer, wPort, NULL, NULL); ASSERT(NULL != pHttpConnection); pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_DELETE, strObject); ASSERT(NULL != pHttpFile); if (!pHttpFile->SendRequest()) goto _err_handler; DWORD dwStateCode; pHttpFile->QueryInfoStatusCode(dwStateCode); if (dwStateCode != HTTP_STATUS_OK) goto _err_handler; bResult = TRUE; } catch (CFileException* e) { e->Delete(); //REPORT_CACHED_EXCEPTION(_T("CFileException")); } catch (CInternetException* e) { CString sError; sError.Format(_T("Inernet connection error : %d"), e->m_dwError); //REPORT_CACHED_EXCEPTION(sError); } _err_handler: if (pHttpFile) { pHttpFile->Close(); delete pHttpFile; } if (pHttpConnection) { pHttpConnection->Close(); delete pHttpConnection; } session.Close(); return bResult; } |
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- HTTP Header 属性列表
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- 如何在 Linux 中快速地通过 HTTP 提供文件访问服务
- 深入HTTP head的使用详解
- Ruby程序中发送基于HTTP协议的请求的简单示例
- ASP 中使用 HTTP 协议发送参数详解
- C#基于socket模拟http请求的方法
- http www安全必备知识
- asp HTTP 500错误 常见问题分析
- Visual C++中MFC消息的分类
- http代理相关知识分析
- MFC中Radio Button的用法详解
- MFC对话框中添加状态栏的方法