MFC 上传图片到http://169.254.1.10:8090/api/v1/~bali/ABC0123456789 有多少种方式
时间: 2023-10-04 08:13:23 浏览: 302
在 MFC 中,上传图片到指定的 URL 可以使用以下几种方式:
1. 使用 WinINet 库进行 HTTP POST 请求,示例代码如下:
```cpp
HINTERNET hSession = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnect(hSession, _T("169.254.1.10"), 8090, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
HINTERNET hRequest = HttpOpenRequest(hConnection, _T("POST"), _T("/api/v1/~bali/ABC0123456789"), NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0);
CString strHeaders = _T("Content-Type: multipart/form-data; boundary=------------------------7d65e3510e5e\n");
CString strFormData = _T("--------------------------7d65e3510e5e\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"test.jpg\"\nContent-Type: image/jpeg\n\n");
CString strFilePath = _T("C:\\test.jpg");
CFile file;
if (file.Open(strFilePath, CFile::modeRead | CFile::typeBinary))
{
DWORD dwFileSize = (DWORD)file.GetLength();
BYTE* pBuffer = new BYTE[dwFileSize];
file.Read(pBuffer, dwFileSize);
DWORD dwBytesWritten;
CString strTail = _T("\n--------------------------7d65e3510e5e--\n");
DWORD dwTailSize = strTail.GetLength() * sizeof(TCHAR);
DWORD dwTotalSize = strFormData.GetLength() * sizeof(TCHAR) + dwFileSize + dwTailSize;
BYTE* pTotalBuffer = new BYTE[dwTotalSize];
memcpy(pTotalBuffer, strFormData, strFormData.GetLength() * sizeof(TCHAR));
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR), pBuffer, dwFileSize);
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR) + dwFileSize, strTail, dwTailSize);
InternetWriteFile(hRequest, pTotalBuffer, dwTotalSize, &dwBytesWritten);
delete[] pBuffer;
delete[] pTotalBuffer;
file.Close();
}
HttpSendRequest(hRequest, strHeaders, strHeaders.GetLength(), NULL, 0);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hSession);
```
2. 使用 WinHTTP 库进行 HTTP POST 请求,示例代码如下:
```cpp
HINTERNET hSession = WinHttpOpen(_T("MyApp"), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET hConnection = WinHttpConnect(hSession, _T("169.254.1.10"), 8090, 0);
HINTERNET hRequest = WinHttpOpenRequest(hConnection, _T("POST"), _T("/api/v1/~bali/ABC0123456789"), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
CString strHeaders = _T("Content-Type: multipart/form-data; boundary=------------------------7d65e3510e5e\n");
CString strFormData = _T("--------------------------7d65e3510e5e\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"test.jpg\"\nContent-Type: image/jpeg\n\n");
CString strFilePath = _T("C:\\test.jpg");
CFile file;
if (file.Open(strFilePath, CFile::modeRead | CFile::typeBinary))
{
DWORD dwFileSize = (DWORD)file.GetLength();
BYTE* pBuffer = new BYTE[dwFileSize];
file.Read(pBuffer, dwFileSize);
DWORD dwBytesWritten;
CString strTail = _T("\n--------------------------7d65e3510e5e--\n");
DWORD dwTailSize = strTail.GetLength() * sizeof(TCHAR);
DWORD dwTotalSize = strFormData.GetLength() * sizeof(TCHAR) + dwFileSize + dwTailSize;
BYTE* pTotalBuffer = new BYTE[dwTotalSize];
memcpy(pTotalBuffer, strFormData, strFormData.GetLength() * sizeof(TCHAR));
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR), pBuffer, dwFileSize);
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR) + dwFileSize, strTail, dwTailSize);
WinHttpSendRequest(hRequest, strHeaders, strHeaders.GetLength(), pTotalBuffer, dwTotalSize, dwTotalSize, 0);
WinHttpReceiveResponse(hRequest, NULL);
delete[] pBuffer;
delete[] pTotalBuffer;
file.Close();
}
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnection);
WinHttpCloseHandle(hSession);
```
3. 使用 MFC 的 CInternetSession 类进行 HTTP POST 请求,示例代码如下:
```cpp
CInternetSession session(_T("MyApp"));
CHttpConnection* pConnection = session.GetHttpConnection(_T("169.254.1.10"), INTERNET_DEFAULT_HTTP_PORT);
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, _T("/api/v1/~bali/ABC0123456789"));
CString strHeaders = _T("Content-Type: multipart/form-data; boundary=------------------------7d65e3510e5e\n");
CString strFormData = _T("--------------------------7d65e3510e5e\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"test.jpg\"\nContent-Type: image/jpeg\n\n");
CString strFilePath = _T("C:\\test.jpg");
CFile file;
if (file.Open(strFilePath, CFile::modeRead | CFile::typeBinary))
{
DWORD dwFileSize = (DWORD)file.GetLength();
BYTE* pBuffer = new BYTE[dwFileSize];
file.Read(pBuffer, dwFileSize);
DWORD dwBytesWritten;
CString strTail = _T("\n--------------------------7d65e3510e5e--\n");
DWORD dwTailSize = strTail.GetLength() * sizeof(TCHAR);
DWORD dwTotalSize = strFormData.GetLength() * sizeof(TCHAR) + dwFileSize + dwTailSize;
BYTE* pTotalBuffer = new BYTE[dwTotalSize];
memcpy(pTotalBuffer, strFormData, strFormData.GetLength() * sizeof(TCHAR));
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR), pBuffer, dwFileSize);
memcpy(pTotalBuffer + strFormData.GetLength() * sizeof(TCHAR) + dwFileSize, strTail, dwTailSize);
pFile->SendRequest(strHeaders, strHeaders.GetLength(), pTotalBuffer, dwTotalSize);
pFile->EndRequest();
delete[] pBuffer;
delete[] pTotalBuffer;
file.Close();
}
pFile->Close();
delete pFile;
delete pConnection;
```
阅读全文