http://www.codeproject.com/KB/IP/simplehttpclient.aspx
Simple HTTP Client using WININET
简介
这个类是用于HTTP请求。它支持的HTTP GET,HTTP POST和HTTP的POST-
MultiPartFormData。
类概述
class GenericHTTPClient {
public:
...
// CONSTRUCTOR & DESTRUCTOR
GenericHTTPClient();
virtual ~GenericHTTPClient();
...
// Connection handler
BOOL Connect( LPCTSTR szAddress,
LPCTSTR szAgent = __DEFAULT_AGENT_NAME,
unsigned short nPort = INTERNET_DEFAULT_HTTP_PORT,
LPCTSTR szUserAccount = NULL,
LPCTSTR szPassword = NULL);
BOOL Close();
VOID InitilizePostArguments();
// HTTP Arguments handler
VOID AddPostArguments(LPCTSTR szName, DWORD nValue);
VOID AddPostArguments(LPCTSTR szName, LPCTSTR szValue,
BOOL bBinary = FALSE);
// HTTP Method handler
BOOL Request( LPCTSTR szURL,
int nMethod = GenericHTTPClient::RequestGetMethod,
LPCTSTR szAgent = __DEFAULT_AGENT_NAME);
BOOL RequestOfURI(LPCTSTR szURI, int nMethod =
GenericHTTPClient::RequestGetMethod);
BOOL Response(PBYTE pHeaderBuffer, DWORD dwHeaderBufferLength,
PBYTE pBuffer, DWORD dwBufferLength, DWORD &dwResultSize);
LPCTSTR QueryHTTPResponse();
LPCTSTR QueryHTTPResponseHeader();
// General Handler
DWORD GetLastError();
LPCTSTR GetContentType(LPCTSTR szName);
VOID ParseURL(LPCTSTR szURL, LPTSTR szProtocol, LPTSTR szAddress,
DWORD &dwPort, LPTSTR szURI);
protected:
...
};
Connect(...) method connects to HTTP Server.
Close() method closes connection. These are used with RequestOfURI(...).
InitilizePostArguments() method initializes post arguments.
AddPostArguments(...) method is supported so that you can add new post arguments of the following 3 types ( TCHAR, DWORD, FILE).
Request(...) method is for you to attempt request for HTTP REQUEST( GET, POST, POST-MULTIPARTFORMDATA) with URL. HTTP METHOD indirector have 3 types.
GenericHTTPClient::GetMethod is HTTP GET REQUEST
GenericHTTPClient::PostMethod is HTTP POST REQUEST
GenericHTTPClient::PostMultiPartsFormData is HTTP POST REQUEST with BINARY FORM DATA
RequestOfURI(...) method is that you could have attempt request for HTTP REQUEST with URI. This method is used with Connect(...) and Close().
Response(...) method is that you have HTTP Response by BYTES.
QueryHTTPResponse() method is you have receive HTML of your HTTP REQUEST.
QueryHTTPResponseHeader() method is you have receive HTTP HEADER about QueryHTTPResponse().
GetLastError() method is you get windows error code.
GetContentType(...) method is you have get MIME-TYPE.
ParseURL(...) method parse URL to protocol(HTTP, HTTPS) and address(or hostname) and port, URI.
使用
现在,你有根本的HTTP GET请求的迭代。
....
GenericHTTPClient httpClient;
if(httpRequest.Request("http://www.codeproject.com")){
LPCTSTR szHTML=httpRequest.QueryResponse();
}else{
LPVOID lpMsgBuffer;
DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
httpRequest.GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
MessageBox( reinterpret_cast<LPTSTR>(lpMsgBuffer), "ERROR", MB_OK);
LocalFree(lpMsgBuffer);
}
This is HTTP POST REQUEST with file posting ( HTTP POST Multipart/form-data) This is HTTP POST REQUEST with file posting ( HTTP POST Multipart/form-data)
GenericHTTPClient *pClient=new GenericHTTPClient();
pClient->InitilizePostArguments();
pClient->AddPostArguments(__TAG_USRID, szUserID);
pClient->AddPostArguments(__TAG_JUMIN, szSocialIndex);
pClient->AddPostArguments(__TAG_SRC, szSource);
pClient->AddPostArguments(__TAG_DST, szDestination);
pClient->AddPostArguments(__TAG_FORMAT, szFormat);
pClient->AddPostArguments(__TAG_SUBJECT, szMessage);
if(bCharge){
pClient->AddPostArguments(__TAG_CHARGE, "Y");
}else{
pClient->AddPostArguments(__TAG_CHARGE, "N");
}
pClient->AddPostArguments(__TAG_CPCODE, szCPCode);
pClient->AddPostArguments(__TAG_FILE, szFile, TRUE);
if(pClient->Request(szURL,
GenericHTTPClient::RequestPostMethodMultiPartsFormData)){
LPCTSTR szResult=pClient->QueryHTTPResponse();
}else{
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
pClient->GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
}
}
分享到:
相关推荐
1.WinInet(这种方式很简单,但不是很灵活) 2.WinSock(也就是Socket,这种方式有点繁琐,但是可以自定义发送HTTP的报文头和接收响应头,很灵活) 因作者编程水平有限,错误之处,在所难免,欢迎批评指正。
1、基于wininet的文件上传 2、测试时可使用hfs作为http服务端程序 3、有封装好的类,可直接使用 4、可能因为字符编码问题,需要自行调整
基于WinInet实现的HTTP文件下载
源代码 报告 可执行文件 利用诸如Java FTP Client Library 组件或VC6.0 WinInet基于国际互连网客户端应用设计接口的程序实现FTP客户端功能。
基于wininet技术集合c++写的源代码,可以实现用代理服务器来访问URL页面,不是vc++,所以页面显示只是HTML的源代码。zhaquanmin@163.com
基于WinInet实现HTTP文件上传
WinInet Http 异步封装类 异步:避免线程安全问题。提供上传下载文件方法
WinInet简单封装示例程序,支持HTTP头、Cookie、附加数据设置发送成功后可以查询返回Cookie、内容、消息头数据。部分函数如下: void ClearHeaders(); void AddHeader(const char* lpszName, const char* lpszValue)...
把基于WinInet的HTTP文件上传功能小程序的开发过程首先,使用 InternetCrackUrl 函数分解URL,从URL中提取网站的域名、路径以及URL的附加信息等。关于 InternetCrackUrl 分解URL的介绍和实现,可以参考 “URL分解之...
VC编程实现使用WinInet通过HTTP协议读取网上文件
WinINet makes network programming easier, but it's difficult to use for C++ because of its C-style interface, so I write this code to wrap it. Using it, you can easily create an asynchronous HTTP ...
基于WININET技术,用c++访问FTP地址,实现上传下载,这个文件写的有点乱。最近比较忙,没有修改就上传了。
Wininet SSL 实现http 数据上传,Wininet SSL 实现http 数据上传
HTTP协议库有两个版本,一个是基于WININET,一个则是使用socket实现的。 可以支持POST 、GET请求,断点下载、重定向下载、下载进度回调,不支持HTTPS。