看流星社区

 找回密码
 注册账号
查看: 2234|回复: 0

MFC向指定网站post数据

[复制链接]

该用户从未签到

发表于 2016-9-21 12:23:38 | 显示全部楼层 |阅读模式
最近有个需求需向指定网站Get数据,用于注册账号。例如:http://baidu.com  Get时需要携带账号和密码信息 如:http://baidu.com?code=abc&password=123456

当然还有经过md5加密过得签名。记得以前有做过类似的需求,网上搜也是一大堆,看的我有些眼花了。以下是我实现用到的两个接口:

CString Get_Http(CString url)  
{  
    CString strLogMsg, strRes;  
    try  
    {  
        CInternetSession session;   
        CHttpFile *pFile = (CHttpFile *)session.OpenURL(url);   
        if ( pFile )  
        {     
            DWORD dwStateCode = 0;  
            pFile->QueryInfoStatusCode(dwStateCode);  
            strLogMsg.Format(_T("Http QueryInfoStatusCode:%ld"), dwStateCode);            
            if (dwStateCode >= 400)  
            {  
                pFile->Close();  
                delete pFile ;  
                return _T("");  
            }  
  
            char buf[1024] = {'\0'};  
            CString str;  
            CString s ;  
  
            UINT uSize = 0;  
  
            UINT nTotal = 0;  
  
            while (true)  
            {  
                memset(buf,0,1024);  
                uSize = pFile->Read(buf,1024);  
                if (uSize>0)  
                {  
                    nTotal += uSize;  
                }  
                if (uSize < 1024 || uSize <= 0)  
                {  
                    break;  
                }  
  
            }  
            buf[nTotal] = '\0';  
            strRes = buf;  
  
            pFile->Close();  
            delete pFile ;  
  
        }  
    }  
    catch( CException *e )  
    {  
        TCHAR   szCause[255];  
        e->GetErrorMessage(szCause, 255);         
        e->Delete();      
    }   
    return strRes;  
}  


BOOL GetMd5(CString strInput, CString &strOutput)  
{   
    HCRYPTPROV hProv=NULL;  
    if(CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT)==FALSE)       //获得CSP中一个密钥容器的句柄  
    {  
        return FALSE;  
    }  
    HCRYPTPROV hHash=NULL;  
    //初始化对数据流的hash,创建并返回一个与CSP的hash对象相关的句柄。这个句柄接下来将被    CryptHashData调用。  
    if(CryptCreateHash(hProv,CALG_MD5,0,0,&hHash)==FALSE)  
    {  
        return FALSE;  
    }  
  
    DWORD lenOfBytes;     
    USES_CONVERSION;  
    char* input = W2A(strInput);  
    lenOfBytes = strlen(input);  
    if(CryptHashData(hHash,(BYTE*)input,lenOfBytes,0)==FALSE)      //hash文件   
    {  
        return FALSE;  
    }         
  
    BYTE *pbHash = NULL;  
    DWORD dwHashLen=sizeof(DWORD);  
    if (!CryptGetHashParam(hHash,HP_HASHVAL,NULL,&dwHashLen,0))      
    {  
        return FALSE;  
    }  
    pbHash=(byte*)malloc(dwHashLen);  
    if(CryptGetHashParam(hHash,HP_HASHVAL,pbHash,&dwHashLen,0))//获得md5值   
    {  
        for(DWORD i=0;i<dwHashLen;i++)         //输出md5值   
        {  
            TCHAR str[1024]={0};  
            CString strFilePartM=_T("");  
            _stprintf(str,_T("%02x"),pbHash);  
            strOutput+=str;  
        }  
    }   
    free(pbHash);  
  
    //善后工作  
    if(CryptDestroyHash(hHash)==FALSE)          //销毁hash对象   
    {  
        return FALSE;            
    }  
    if(CryptReleaseContext(hProv,0)==FALSE)  
    {  
        return FALSE;         
    }  
    return TRUE;  
}   

最近在网上还找到了一个用c++封装的get和post的类,顺便贴出来下:

// CHttpClient 命令目标  
#include <afxinet.h>  
#include <string>  
using namespace std ;  
   
#define  IE_AGENT  _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")  
   
// 操作成功  
#define SUCCESS        0  
// 操作失败  
#define FAILURE        1  
// 操作超时  
#define OUTTIME        2  
   
class CHttpClient : public CObject  
{  
public :  
    CHttpClient (LPCTSTR strAgent = IE_AGENT);  
    virtual ~CHttpClient ( void);  
   
    int HttpGet ( LPCTSTR strUrl , LPCTSTR strPostData , string & strResponse );  
    int HttpPost ( LPCTSTR strUrl , LPCTSTR strPostData , string & strResponse );  
    //,该函数用来对url进行编码。将非ASCII字符转化成"%XX%XX%XX"格式,isWide和isChangeEqualAndSlash分别用来决定是否将获取到的内容转成宽字符和是否转换'='和'/'转换出的url按照UTF-8编码  
    static CString CHttpClient :: UrlEncode (CString strUnicode ,LPCTSTR url= NULL ,BOOL isWide =FALSE , BOOL isChangeEqualAndSlash= FALSE );  
private :  
    int ExecuteRequest ( LPCTSTR strMethod , LPCTSTR strUrl , LPCTSTR strPostData, string&strResponse );  
    void Clear ();  
   
private :  
    CInternetSession * m_pSession ;  
    CHttpConnection * m_pConnection ;  
    CHttpFile *m_pFile ;  
};  
  


#include "stdafx.h"  
#include "MyHttp.h"  
   
#define  BUFFER_SIZE  4*1024  
   
#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  
   
CString CHttpClient ::UrlEncode ( CString strUnicode ,LPCTSTR url ,BOOL isWide, BOOL isChangeEqualAndSlash )  
{  
    LPCWSTR unicode = T2CW( strUnicode );  
     int len = WideCharToMultiByte ( CP_UTF8 , 0, unicode , - 1, 0 , 0 , 0 , 0 );  
     if (! len)  
         return strUnicode ;  
     char *utf8 = new char [ len + 1 ];  
     char *utf8temp = utf8;  
    WideCharToMultiByte ( CP_UTF8 , 0, unicode , - 1, utf8 , len + 1 , 0 , 0 );  
    utf8 [len ] = 0 ;     
    CString strTemp , strEncodeData ;  
     if (url != NULL)  
        strEncodeData = url;  
      
     while ((* utf8) != 0)  
     {  
         if ((*utf8 )<= 127&&(* utf8 )>0 &&(* utf8)!= '+' &&(*utf8 )!= ' ' &&(*utf8 )!= '?'  
             &&(* utf8)!= '%' &&(*utf8 )!= '#'&&(! isChangeEqualAndSlash ||((*utf8 )!= '='&&(* utf8 )!='/' )))  
            strEncodeData +=( char)(* utf8 );  
         else {  
            strTemp . Format( _T ("%%%2x" ), ( BYTE )*utf8 );  
            strEncodeData += strTemp ;  
         }  
         ++ utf8;  
     }  
   
     delete [] utf8temp ;  
     if (isWide ==FALSE )  
     {  
        CStringA stra ( strEncodeData .GetBuffer (0 ));  
         return ( LPCWSTR )stra . GetBuffer (0 );  
     } else return strEncodeData ;  
   
}  
   

// CHttpClient 成员函数  
CHttpClient ::CHttpClient ( LPCTSTR strAgent )  
{  
    m_pSession = new CInternetSession (strAgent );  
    m_pConnection = NULL ;  
    m_pFile = NULL ;  
}  
   
CHttpClient ::~CHttpClient ( void)  
{  
    Clear ();  
     if (NULL != m_pSession )  
     {  
        m_pSession -> Close();  
         delete m_pSession ;  
        m_pSession = NULL ;  
     }  
}  
   
void CHttpClient ::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 CHttpClient ::ExecuteRequest (LPCTSTR strMethod , LPCTSTR strUrl , LPCTSTR strPostData, string&strResponse )  
{  
    CString strServer ;  
    CString strObject ;  
    DWORD dwServiceType ;  
    INTERNET_PORT nPort ;  
    strResponse = "" ;  
   
    AfxParseURL ( strUrl, dwServiceType , strServer , strObject , nPort );  
   
     if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType )  
     {  
         return FAILURE ;  
     }  
   
     try  
     {  
        m_pConnection = m_pSession ->GetHttpConnection (strServer ,  
            dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,  
            nPort );  
        m_pFile = m_pConnection ->OpenRequest (strMethod , strObject ,  
             NULL , 1 , NULL , NULL ,  
             ( dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));  
   
         //DWORD dwFlags;  
         //m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);  
         //dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;  
         ////set web server option  
         //m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);  
   
        m_pFile -> AddRequestHeaders (_T ("Accept: *,*/*" ));  
        m_pFile -> AddRequestHeaders (_T ("Accept-Language: zh-cn" ));  
        m_pFile -> AddRequestHeaders (_T ("Content-Type: application/x-www-form-urlencoded" ));  
        m_pFile -> AddRequestHeaders (_T ("Accept-Encoding: deflate")); //gzip,  
   
        m_pFile -> SendRequest (NULL , 0 , ( LPVOID)( LPCTSTR )strPostData , strPostData == NULL ? 0: _tcslen( strPostData ));  
        char szChars [BUFFER_SIZE + 1 ] = { 0 };  
        string strRawResponse = "" ;  
        UINT nReaded = 0 ;  
         while (( nReaded = m_pFile -> Read(( void *)szChars , BUFFER_SIZE )) > 0 )  
         {  
             szChars [ nReaded ] = '\0' ;  
            strRawResponse += szChars ;  
            memset ( szChars , 0, BUFFER_SIZE + 1 );  
         }  
   
         int unicodeLen = MultiByteToWideChar (CP_UTF8 , 0 , strRawResponse. c_str(), -1 , NULL , 0 );  
        WCHAR * pUnicode = new WCHAR [ unicodeLen + 1];  
        memset ( pUnicode ,0 ,(unicodeLen + 1)* sizeof (wchar_t ));  
   
        MultiByteToWideChar ( CP_UTF8 ,0 ,strRawResponse . c_str(),- 1 , pUnicode, unicodeLen );  
        CString cs ( pUnicode );  
         //AfxMessageBox(cs);  
        CStringA stra ( cs. GetBuffer (0 ));  
        strResponse = stra. GetBuffer (0 ); //(LPCSTR)(LPCWSTR)cs;  
        stra . ReleaseBuffer ();  
         delete [] pUnicode ;  
        pUnicode = NULL ;  
        Clear ();  
     }  
     catch ( CInternetException * e )  
     {  
        Clear ();  
        DWORD dwErrorCode = e-> m_dwError ;  
        e -> Delete();  
   
        DWORD dwError = GetLastError ();  
   
         //PRINT_LOG("dwError = %d", dwError, 0);  
   
         if ( ERROR_INTERNET_TIMEOUT == dwErrorCode )  
         {  
             return OUTTIME ;  
         }  
         else  
         {  
             return FAILURE ;  
         }  
     }  
     return SUCCESS ;  
}  


int CHttpClient ::HttpGet (LPCTSTR strUrl , LPCTSTR strPostData , string & strResponse )  
{  
     return ExecuteRequest ( _T( "GET" ), strUrl , strPostData , strResponse );  
}  


int CHttpClient ::HttpPost (LPCTSTR strUrl , LPCTSTR strPostData , string & strResponse )  
{  
     return ExecuteRequest ( _T( "POST" ), strUrl , strPostData , strResponse );  
}  
  
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 14:03

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表