频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
c++发送http请求chttpcile代码实例
2018-03-12 10:22:39         来源:qq_28039867的博客  
收藏   我要投稿

直接贴代码 但是发送中文时 是gb2312的url编码不是utf-8的

/** 
 * \brief 对url特殊字符进行编码 
 * \param s 输入字符串 
 * \param len 输入字符串长度 
 * \param new_length 输出字符串长度 
 * \return 输出编码后的url字符串,这段内存再使用完成以后需要释放 
 */  
std::string urlencode(std::string &str_source)  
{  
    char const *in_str = str_source.c_str();  
    int in_str_len = strlen(in_str);  
    int out_str_len = 0;  
    std::string out_str;  
    register unsigned char c;  
    unsigned char *to, *start;  
    unsigned char const *from, *end;  
    unsigned char hexchars[] = "0123456789ABCDEF";  
  
    from = (unsigned char *)in_str;  
    end = (unsigned char *)in_str + in_str_len;  
    start = to = (unsigned char *) malloc(3*in_str_len+1);  
  
    while (from < end) {  
        c = *from++;  
  
        if (c == ' ') {  
            *to++ = '+';  
        } else if ((c < '0' && c != '-' && c != '.') ||  
            (c < 'A' && c > '9') ||  
            (c > 'Z' && c < 'a' && c != '_') ||  
            (c > 'z')) {   
                to[0] = '%';  
                to[1] = hexchars[c >> 4];  
                to[2] = hexchars[c & 15];  
                to += 3;  
        } else {  
            *to++ = c;  
        }  
    }  
    *to = 0;  
  
    out_str_len = to - start;  
    out_str = (char *) start;  
    free(start);  
    return out_str;  
}  
std::string urlpost(std::string &str_source)  
{  
    char *a = "data=";
	std::string str = std::string(a) + urlencode(str_source);
    return str;  
}  
CString http_request(CString server, CString uri,int port,CHAR* strFormData){
	//设置session
	CInternetSession session(_T("My Session")); 
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	//设置报头
	CString szHeaders( "Content-Type: application/x-www-form-urlencoded;");
	CString strObject(uri); 
	CString out;
	DWORD dwRet;


char *szBuff = new char[];
memset(szBuff, 0, sizeof(szBuff));

try
{

    CString strServerName(server);
   INTERNET_PORT nPort(port);

   pServer = session.GetHttpConnection(strServerName, nPort);
   //设置发送类型
   pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
   //设置报头
   pFile->AddRequestHeaders(szHeaders);
   //发送请求
   pFile->SendRequest(szHeaders, szHeaders.GetLength(),(LPVOID)strFormData, strlen(strFormData));
   //获取服务端返回数据
   pFile->QueryInfoStatusCode(dwRet);
   //读取返回数据
   if (dwRet == HTTP_STATUS_OK)
   {
       UINT nRead = pFile->Read(szBuff, sizeof(szBuff));
       //while (nRead > 0)
       //{
           out = CString(szBuff);
       //}
   } else {
        out = CString("Communication error!");
   }
   //删除file
   delete pFile;
   //关闭服务
   pServer->Close()
	//删除服务;
   delete pServer;
}
catch (CInternetException* pEx)
{
    //catch errors from WinInet

    out = CString("Network error!"); //
}
//关闭session
session.Close(); 
return out;
} 
点击复制链接 与好友分享!回本站首页
上一篇:C++设计模式之原型模式(克隆羊多莉)实例讲解
下一篇:c++发送http请求winnet的代码实例
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站