|
|
#pragma once
|
|
|
#include <iostream>
|
|
|
#include <windows.h>
|
|
|
#include <wininet.h>
|
|
|
#include <map>
|
|
|
#include <functional>
|
|
|
using namespace std;
|
|
|
|
|
|
//每次读取的字节数
|
|
|
#define READ_BUFFER_SIZE 4096
|
|
|
|
|
|
enum HttpInterfaceError
|
|
|
{
|
|
|
Hir_Success = 0, //成功
|
|
|
Hir_InitErr, //初始化失败
|
|
|
Hir_ConnectErr, //连接HTTP服务器失败
|
|
|
Hir_SendErr, //发送请求失败
|
|
|
Hir_QueryErr, //查询HTTP请求头失败
|
|
|
Hir_404, //页面不存在
|
|
|
Hir_IllegalUrl, //无效的URL
|
|
|
Hir_CreateFileErr, //创建文件失败
|
|
|
Hir_DownloadErr, //下载失败
|
|
|
Hir_QueryIPErr, //获取域名对应的地址失败
|
|
|
Hir_SocketErr, //套接字错误
|
|
|
Hir_UserCancel, //用户取消下载
|
|
|
Hir_BufferErr, //文件太大,缓冲区不足
|
|
|
Hir_HeaderErr, //HTTP请求头错误
|
|
|
Hir_ParamErr, //参数错误,空指针,空字符
|
|
|
Hir_UnknowErr,
|
|
|
};
|
|
|
enum HttpRequest
|
|
|
{
|
|
|
Hr_Get,
|
|
|
Hr_Post
|
|
|
};
|
|
|
|
|
|
class CWininetHttp
|
|
|
{
|
|
|
public:
|
|
|
//单例模式
|
|
|
static CWininetHttp& getInstance()
|
|
|
{
|
|
|
static CWininetHttp m_winHttp;
|
|
|
return m_winHttp;
|
|
|
}
|
|
|
CWininetHttp(const CWininetHttp&) = delete;
|
|
|
CWininetHttp& operator =(const CWininetHttp&) = delete;
|
|
|
~CWininetHttp(void);
|
|
|
|
|
|
//同步获取http请求
|
|
|
string synRequestInfo(const string& lpUrl, HttpRequest type, map<string, string> param = map<string, string>());
|
|
|
//异步获取http请求
|
|
|
void asynRequestInfo(const string& lpUrl, HttpRequest type, map<string, string> param, std::function<void(string)> callBack);
|
|
|
private:
|
|
|
CWininetHttp(void);
|
|
|
// 通过HTTP请求:Get或Post方式获取JSON信息 [3/14/2017/shike]
|
|
|
const std::string RequestJsonInfo(const std::string& strUrl,
|
|
|
HttpRequest type = Hr_Get,
|
|
|
std::string lpHeader = "",
|
|
|
std::string lpPostData = "");
|
|
|
|
|
|
string getUrl(const string& lpUrl, map<string, string> param);
|
|
|
|
|
|
void asynRequestThread(string totalUrl, HttpRequest type);
|
|
|
|
|
|
// 关闭句柄
|
|
|
void Release();
|
|
|
|
|
|
// 释放句柄
|
|
|
void ReleaseHandle(HINTERNET& hInternet);
|
|
|
|
|
|
// 解析URL地址
|
|
|
void ParseURLWeb(std::string strUrl, std::string& strHostName, std::string& strPageName, WORD& sPort);
|
|
|
|
|
|
// UTF-8转为GBK2312
|
|
|
char* UtfToGbk(const char* utf8);
|
|
|
|
|
|
private:
|
|
|
HINTERNET m_hSession;
|
|
|
HINTERNET m_hConnect;
|
|
|
HINTERNET m_hRequest;
|
|
|
HttpInterfaceError m_error;
|
|
|
std::function<void(string)> callBack = NULL;
|
|
|
};
|