设为首页 加入收藏

TOP

c++发送http请求chttpcile代码实例
2018-03-13 09:06:32 】 浏览:81
Tags:发送 http 请求 chttpcile 代码 实例

直接贴代码 但是发送中文时 是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的代码实例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目