1、PostConnect(CString url,const CString &strPara,CString &strContent, CString &strDescript)发送Http请求.url:要请求的地址,
strPara请求的参数.strContent 服务器返回的信息.strDescript程序执行信息.
2、可能请求中的参数有存在中文字符的关系,可能会存在乱码,所以附带了2个转码的方法。
下面贴出代码:
PostConn.h
#pragma once #include "afxinet.h" class CPostConn { public: CPostConn(); ~CPostConn(); public: bool PostConnect(CString url,const CString &strPara,CString &strContent, CString &strDescript); void GBKToUTF8(CString &strGBK); void UTF8ToGBK(CString &str); };
PostConn.cpp
#include "stdafx.h" #include "PostConn.h" CPostConn::CPostConn() { } CPostConn::~CPostConn() { } bool CPostConn::PostConnect(CString url,const CString &strPara,CString &strContent, CString &strDescript) { try { strDescript="提交成功完成"; bool bRet=false; CString strServer, strObject, strHeader, strRet; unsigned short nPort; DWORD dwServiceType; if(!AfxParseURL(url,dwServiceType,strServer,strObject,nPort)) { strDescript="不是有效的网络地址"; return false; } CInternetSession sess; CHttpFile *pFile; CHttpConnection *pServer=sess.GetHttpConnection(strServer,nPort); if(pServer==NULL) { strDescript = "对不起,连接服务器失败!"; return false; } pFile=pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObject,NULL,1, NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT); if(pFile == NULL) { strDescript = "找不到网络地址" + url; return false; } CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); pFile->AddRequestHeaders(strHeaders); pFile->AddRequestHeaders("Accept: */*"); pFile ->SendRequest(NULL,0,(LPTSTR)(LPCTSTR)strPara, strPara.GetLength()); CString strSentence; DWORD dwStatus; DWORD dwBuffLen = sizeof(dwStatus); BOOL bSuccess = pFile->QueryInfo( HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen); if( bSuccess && dwStatus>= 200 && dwStatus<300) { char buffer[256]; memset(buffer, 0, 256); int nReadCount = 0; while((nReadCount = pFile->Read(buffer, 2048)) > 0) { strContent += buffer; memset(buffer, 0, 256); } bRet = true; } else { strDescript = "网站服务器错误" + url; bRet = false; } //////////////////////////////////////// pFile->Close(); sess.Close(); return bRet; } catch (...) { int nCode = GetLastError(); strDescript.Format("向服务器post失败!错误号:%d", nCode); return false; } } void CPostConn::GBKToUTF8(CString &strGBK) { WCHAR * str1; int n = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0); str1 = new WCHAR[n]; MultiByteToWideChar(CP_ACP, 0, strGBK, -1, str1, n); n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); char * str2 = new char[n]; WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); strGBK = str2; delete[]str1; str1 = NULL; delete[]str2; str2 = NULL; } void CPostConn::UTF8ToGBK(CString &str) { WCHAR* strSrc; TCHAR* szRes; int i=MultiByteToWideChar(CP_UTF8,0,str,-1,NULL,0); strSrc=new WCHAR[i=1]; MultiByteToWideChar(CP_UTF8,0,str,-1,strSrc,i); i=WideCharToMultiByte(CP_ACP,0,strSrc,-1,NULL,0,NULL,NULL); szRes=new TCHAR[i]; WideCharToMultiByte(CP_ACP,0,strSrc,-1,szRes,i,NULL,NULL); str=szRes; delete []strSrc; delete []szRes; }
记录下来,方便以后.