资源简介
在网上找的大都是多字节的工程代码,传参以及返回参数中有中文都会乱码。现在VC工程几乎都是Unicode字符集,这个类已经完全解决了中文乱码问题,并且支持Get和Post请求,在VS2015下已经测试通过,注释详细,有使用示例。

代码片段和文件信息
/************************************************************************
使用示例(测试网址是自己写的WebService接口):
1. get
CString strUrl1;
strUrl1 = L“http://192.168.2.179:9999/WebService1.asmx“;
strUrl1 += L“/ShowStringParam?userName=老王&userAge=42“;
std::string strGetInfo1(““);
CWebCommand webCommand1;
int nRet1 = webCommand1.HttpGet(strUrl1 strGetInfo1);
2. post
CString strUrl2;
strUrl2 = L“http://192.168.2.179:9999/WebService1.asmx“;
strUrl2 += L“/ShowStringParam“;
std::string strGetInfo2;
CWebCommand webCommand2;
webCommand2.AddParam(_T(“userName“) _T(“老王“));
webCommand2.AddParam(_T(“userAge“) _T(“42“));
int nRet2 = webCommand2.HttpPost(strUrl2 strGetInfo2);
************************************************************************/
#include “stdafx.h“
#include “WebCommand.h“
#include
#pragma comment(lib “Ws2_32.lib“)
#include
#pragma comment(lib “comsuppw.lib“)
using namespace _com_util;
#define CONNECTON_TIMEOUT 4000 // 超时标准
#define BUFFER_SIZE (4 * 1024) // 接收数据buffer size
#define NORMAL_CONNECT INTERNET_FLAG_KEEP_CONNECTION
#define SECURE_CONNECT NORMAL_CONNECT | INTERNET_FLAG_SECURE
#define NORMAL_REQUEST INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE
#define SECURE_REQUEST NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
CWebCommand::CWebCommand(LPCTSTR strAgent) :
m_pConnection(NULL)
m_pFile(NULL)
m_strPostData(““)
{
m_pSession = new CInternetSession(strAgent);
}
CWebCommand::~CWebCommand()
{
Clear();
if (NULL != m_pSession)
{
m_pSession->Close();
delete m_pSession;
m_pSession = NULL;
}
}
int CWebCommand::HttpGet(CString strUrl string &strResponse)
{
return ExecuteGetRequest(strUrl strResponse);
}
int CWebCommand::HttpPost(CString strUrl string &strResponse)
{
return ExecutePostRequest(strUrl strResponse);
}
void CWebCommand::AddParam(CString strName CString strVaule)
{
m_strPostData = m_strPostData + strName + _T(“=“) + strVaule + _T(“&“);
}
void CWebCommand::Clear()
{
if (NULL != m_pFile)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
}
if (NULL != m_pConnection)
{
m_pConnection->Close();
delete m_pConnection;
m_pConnection = NULL;
}
}
int CWebCommand::ExecuteGetRequest(CString strUrl string & strResponse)
{
try
{
// 1. 首先得设置个超时标准吧
m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT CONNECTON_TIMEOUT);
// 2. request to the HTTP server
ConvertUnicodeToUtf8(strUrl); // before OpenUrl convert strUrl coding
m_pFile = (CHttpFile*)m_pSession->OpenURL(strUrl);
if (m_pFile)
{
char szChars[BUFFER_SIZE + 1] = { 0 }; // buffer
UINT nReaded = 0; // The number of bytes transferred to the buffer
while ((nReaded = m_pFile->Read((void *)szChars BUFFER_SIZE)) > 0)
{
szChars[nReaded] = ‘\0‘;
strResponse += szChars;
}
ConvertUtf8ToUnicode(strRespons
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-03-31 08:57 WebCommand\
文件 14248 2017-04-01 17:43 WebCommand\WebCommand.cpp
文件 2748 2017-04-01 17:43 WebCommand\WebCommand.h
相关资源
- http请求状态代码
- Windows异步套接字网络编程
- VC 获得文件属性 获取文件的创建时
- 基于MVC模式的会员管理系统
- silicon lab公司的收音IC SI47XX全套开发工
- 读者写者问题(读者优先,写者优先
- MFC程序-碰撞的小球
- vc 柱形图 CBarChart
- 用vc 写的导线测量,针对刚学测绘的
- 用VC 编写的仿QQ聊天室程序源代码
- 栅栏填充算法源码(VC)
- UNICODE GBK双向码表二进制文件
- 简单的房屋租赁系统
- .net网站服装销售系统(MVC)
-
ob
jectARX给Auto CAD加工具条 - blowfish的vc2008工程.rar
- QT 实现文件下载
- 画图程序MFC/VC/VC CRectTracker 串行化
- capon波束形成算法-VC实现
- 常用编码(UnicodeUTF-8GBK)转换工具
- 读取串口数据并画实时曲线的VC 程序
- VC 游戏编程—附源代码
- IpHlpApi.h&IpHlpApi.lib
- vc 6.0开发的流程图编辑器
- VC 天空盒(skyBox)实现(附源代码)
- c MFC 画多边形
- keil vcom windows 7 64bit 驱动
- vc URL编解码类
- vc编写中国象棋详细源码注释并附有视
- VC 围棋源代码
评论
共有 条评论