VC实现Http Put方法
2015-09-30 10:59
676 查看
http://blog.csdn.net/mrxyz098/article/details/27991335
该函数可用于向服务器上传文件。服务器端可以有专门的接收机制,也可以没有。
#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")
BOOL HttpRequestPut(LPCTSTR pHomeUrl, LPCTSTR pPageUrl, LONG nPort,
LPCTSTR pFile, CString *psRes, PBOOL pbExit)
{
LPINTERNET_BUFFERS pBufferIn = new INTERNET_BUFFERS;
ZeroMemory(pBufferIn, sizeof(INTERNET_BUFFERS));
LPBYTE pBuf = new BYTE[1024];
HINTERNET hInternet = NULL;
HINTERNET hSession = NULL;
HINTERNET hRequest = NULL;
DWORD dwBytesRead;
DWORD dwBytesWritten;
HANDLE hFile = INVALID_HANDLE_VALUE;
CString sFormat(_T("Connection: Keep-Alive\r\n"));
sFormat += _T("Content-Type: application/octet-stream\r\n");
sFormat += _T("Content-Length: %u\r\n");
sFormat += _T("User-Agent:Test\r\n");
sFormat += _T("Host: %s:%u\r\n");
sFormat += _T("Accept: *.*,*/*\r\n");
sFormat += _T("\r\n");
CString sHeader(_T(""));
CString sRes(_T(""));
do
{
hInternet = InternetOpen(_T("Mozilla/4.0 (compatible; Indy Library)"),
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if( NULL == hInternet )
{
sRes.Format(_T("Open link error. ErrCode=[%u]"), GetLastError());
break;
}
hSession = InternetConnect(hInternet, pHomeUrl, (INTERNET_PORT)nPort,
NULL, NULL,INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
hRequest = HttpOpenRequest(hSession, _T("PUT"), pPageUrl,
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if( FALSE == hRequest )
{
sRes.Format(_T("Open request handle error. ErrCode=[%u]"),
GetLastError());
break;
}
hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile == INVALID_HANDLE_VALUE )
{
sRes.Format(_T("Open File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
pBufferIn->dwStructSize = sizeof(INTERNET_BUFFERS);
pBufferIn->dwBufferTotal = GetFileSize(hFile, NULL);
sHeader.Format(sFormat, pBufferIn->dwBufferTotal, pHomeUrl, nPort);
pBufferIn->lpcszHeader = sHeader;
pBufferIn->dwHeadersLength = sHeader.GetLength();
if( FALSE ==
HttpSendRequestEx(hRequest, pBufferIn, NULL, HSR_INITIATE, 0) )
{
sRes.Format(_T("Send request error."));
break;
}
DWORD dwSendSize = 0;
while( dwSendSize < pBufferIn->dwBufferTotal )
{
if( (NULL!=pbExit) && (FALSE!=(*pbExit)) )
{
sRes.Format(_T("Stop upload because receive exit cmd."));
break;
}
if( FALSE == ReadFile( hFile, pBuf, 1024, &dwBytesRead, NULL) )
{
sRes.Format(_T("Read File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
if( FALSE == InternetWriteFile(hRequest, pBuf, dwBytesRead,
&dwBytesWritten) )
{// ERROR_INTERNET_CONNECTION_ABORTED
sRes.Format(_T("Upload File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
dwSendSize += dwBytesWritten;
}
if( FALSE == HttpEndRequest(hRequest, NULL, 0, 0) )
{
sRes.Format(_T("End request error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
}
}while(FALSE);
if( hFile != INVALID_HANDLE_VALUE )
{
CloseHandle(hFile);
}
if( hRequest )
{
InternetCloseHandle(hRequest);
}
if( hSession )
{
InternetCloseHandle(hSession);
}
if( hInternet )
{
InternetCloseHandle(hInternet);
}
delete []pBuf;
delete pBufferIn;
if( NULL != psRes )
{
*psRes = sRes;
}
return sRes.IsEmpty();
}
该函数可用于向服务器上传文件。服务器端可以有专门的接收机制,也可以没有。
#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")
BOOL HttpRequestPut(LPCTSTR pHomeUrl, LPCTSTR pPageUrl, LONG nPort,
LPCTSTR pFile, CString *psRes, PBOOL pbExit)
{
LPINTERNET_BUFFERS pBufferIn = new INTERNET_BUFFERS;
ZeroMemory(pBufferIn, sizeof(INTERNET_BUFFERS));
LPBYTE pBuf = new BYTE[1024];
HINTERNET hInternet = NULL;
HINTERNET hSession = NULL;
HINTERNET hRequest = NULL;
DWORD dwBytesRead;
DWORD dwBytesWritten;
HANDLE hFile = INVALID_HANDLE_VALUE;
CString sFormat(_T("Connection: Keep-Alive\r\n"));
sFormat += _T("Content-Type: application/octet-stream\r\n");
sFormat += _T("Content-Length: %u\r\n");
sFormat += _T("User-Agent:Test\r\n");
sFormat += _T("Host: %s:%u\r\n");
sFormat += _T("Accept: *.*,*/*\r\n");
sFormat += _T("\r\n");
CString sHeader(_T(""));
CString sRes(_T(""));
do
{
hInternet = InternetOpen(_T("Mozilla/4.0 (compatible; Indy Library)"),
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if( NULL == hInternet )
{
sRes.Format(_T("Open link error. ErrCode=[%u]"), GetLastError());
break;
}
hSession = InternetConnect(hInternet, pHomeUrl, (INTERNET_PORT)nPort,
NULL, NULL,INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
hRequest = HttpOpenRequest(hSession, _T("PUT"), pPageUrl,
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if( FALSE == hRequest )
{
sRes.Format(_T("Open request handle error. ErrCode=[%u]"),
GetLastError());
break;
}
hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile == INVALID_HANDLE_VALUE )
{
sRes.Format(_T("Open File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
pBufferIn->dwStructSize = sizeof(INTERNET_BUFFERS);
pBufferIn->dwBufferTotal = GetFileSize(hFile, NULL);
sHeader.Format(sFormat, pBufferIn->dwBufferTotal, pHomeUrl, nPort);
pBufferIn->lpcszHeader = sHeader;
pBufferIn->dwHeadersLength = sHeader.GetLength();
if( FALSE ==
HttpSendRequestEx(hRequest, pBufferIn, NULL, HSR_INITIATE, 0) )
{
sRes.Format(_T("Send request error."));
break;
}
DWORD dwSendSize = 0;
while( dwSendSize < pBufferIn->dwBufferTotal )
{
if( (NULL!=pbExit) && (FALSE!=(*pbExit)) )
{
sRes.Format(_T("Stop upload because receive exit cmd."));
break;
}
if( FALSE == ReadFile( hFile, pBuf, 1024, &dwBytesRead, NULL) )
{
sRes.Format(_T("Read File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
if( FALSE == InternetWriteFile(hRequest, pBuf, dwBytesRead,
&dwBytesWritten) )
{// ERROR_INTERNET_CONNECTION_ABORTED
sRes.Format(_T("Upload File error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
break;
}
dwSendSize += dwBytesWritten;
}
if( FALSE == HttpEndRequest(hRequest, NULL, 0, 0) )
{
sRes.Format(_T("End request error. ErrCode=[%u] File=[%s]"),
GetLastError(), pFile);
}
}while(FALSE);
if( hFile != INVALID_HANDLE_VALUE )
{
CloseHandle(hFile);
}
if( hRequest )
{
InternetCloseHandle(hRequest);
}
if( hSession )
{
InternetCloseHandle(hSession);
}
if( hInternet )
{
InternetCloseHandle(hInternet);
}
delete []pBuf;
delete pBufferIn;
if( NULL != psRes )
{
*psRes = sRes;
}
return sRes.IsEmpty();
}
相关文章推荐
- VC使用Http Get方法获取页面内容
- 1、checklistbox 用法总结 http://blog.sina.com.cn/s/blog_4cf58c9c010106mx.html
- TCP/IP 网络基础
- 那些年我们追过的网络库(PartI)---(转载)
- TCP/IP协议原理与应用笔记10:TCP/IP协议族
- Android网络编程之URLConnection和HttpClient访问服务器
- 去掉 AD13 PCB网络连线中的双斜杠
- 网络通信---端口重用(如何解除bind绑定)
- doPost或doGet调用出错(状态代码为405) : HTTP method GET is not supported by this URL
- TCP/IP协议原理与应用笔记09:数据通信---封装(邮政系统层次结构:便于理解OSI网络模型)
- Java学习之网络编程
- HttpClientUtil(交互三方接口)
- 动手学习TCP:TCP连接建立与终止
- linux网络编程--跳水send和recv
- 【TCP/IP 合约】 TCP/IP 基金会
- http请求报文介绍
- TCP/IP协议原理与应用笔记08:对等层和对等实体
- Linux下关于system调用 http://blog.chinaunix.net/uid-21768364-id-3995764.html
- js+html+css简单的互动功能页面(2015知道几乎尖笔试题)http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html?from=y1.7-1.2
- http://www.eclipse.org/m2e/ Maven