xuebing1995
离水的鱼儿
采纳率100%
2017-12-01 08:34 阅读 1.5k
已采纳

MFC POST 请求数据中文乱码

我POST中文后用服务器后台看中文是乱码的,数字和英文是没问题的。我转码也不管事,要疯了!!!求大神来告知一二~~~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    qq_23126581 郭老师的小迷弟雅思莫了 2017-12-01 08:40

    是取数据看到中文乱码吗?编码一般设置为UTF-8

    点赞 评论 复制链接分享
  • xuebing1995 离水的鱼儿 2017-12-04 02:06

    // HttpClient.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    // #include "emailsenderv2.h"

    #include "HttpClient.h"

    #ifdef DEBUG

    #undef THIS_FILE

    static char THIS_FILE[]=
    _FILE__;

    #define new DEBUG_NEW

    #endif

    //////////////////////////////////////////////////////////////////////

    // Construction/Destruction

    //////////////////////////////////////////////////////////////////////

    CHttpClient::CHttpClient()

    {

    }

    CHttpClient::~CHttpClient()

    {

    }

    CString CHttpClient::doGet(CString href)

    {

    CString httpsource=""; //接收http返回的数据
    CInternetSession session1(NULL,0);

    CHttpFile* pHTTPFile=NULL;

    try{

    pHTTPFile=(CHttpFile*)session1.OpenURL(href);

    //session1.

    }catch(CInternetException){

    pHTTPFile=NULL;

    }

    if(pHTTPFile)

    {

    CString text;

    for(int i=0;pHTTPFile->ReadString(text);i++)

    {

    httpsource=httpsource+text+"/r/n";

    }

    pHTTPFile->Close();

    delete pHTTPFile;

    }else

    {

    }  
    
    //将返回的数据转换成UTF-8格式
    LPSTR pszOutBuffer=httpsource.GetBuffer();
    httpsource.ReleaseBuffer();//释放缓存
    
    wchar_t *pwtext=NULL;
    DWORD dwNum=MultiByteToWideChar(CP_ACP,0,pszOutBuffer,-1,NULL,0);//返回底ASCII码的字符数目
    pwtext=new wchar_t[dwNum];              //根据ASCII码的字符数分配UTF8的空间
    MultiByteToWideChar(CP_UTF8,0,pszOutBuffer,-1,pwtext,dwNum);    //将ASCII码抓换成UTF-8
    
    httpsource=pwtext;
    
    return   httpsource;   //返回utf8格式http数据
    

    }

    //post提交方式
    CString CHttpClient::doPost(CString href)

    {

    CString httpsource="";

    CInternetSession session1;

    CHttpConnection* conn1=NULL;

    CHttpFile* pFile = NULL;

    CString strServerName;

    CString strObject;

    INTERNET_PORT nPort;

    DWORD dwServiceType;

    AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort);

    DWORD retcode;

    char* outBuff = CONTENT.GetBuffer(1000);
    CString strHeaders="";

    try       
    {                   
        conn1   =   session1.GetHttpConnection(strServerName,nPort);           
        pFile   =   conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);         
        pFile   ->   AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");  
        // 
        pFile   ->   AddRequestHeaders("Accept:   */*");  
        //pFile ->   AddRequestHeaders("Accept-Language:zh-cn");
        //pFile ->   AddRequestHeaders("Accept-Encoding:gzip,deflate");
        //pFile   ->   SendRequest(NULL,0,text,dwNum1+1);     
        pFile   ->   SendRequest(NULL,0,outBuff,strlen(outBuff)+1);               
        pFile   ->   QueryInfoStatusCode(retcode);                   
    }                             
    catch   (CInternetException   *   e)
    {
        DWORD dwError=e->m_dwError;
    };                   
    if(pFile)   
    {   
        CString   text;   
        for(int   i=0;pFile->ReadString(text);i++)   
        {   
            httpsource=httpsource+text+"/r/n";   
        }   
        pFile->Close();   
    }else   
    {   
    
    }   
    
    /*LPSTR pszOutBuffer=httpsource.GetBuffer();
    httpsource.ReleaseBuffer();//释放缓存*/
    
    wchar_t *pwtext=NULL;
    DWORD dwNum=MultiByteToWideChar(CP_ACP,0,httpsource,-1,NULL,0);//返回底ASCII码的字符数目
    pwtext=new wchar_t[dwNum];              //根据ASCII码的字符数分配UTF8的空间
    MultiByteToWideChar(CP_UTF8,0,httpsource,-1,pwtext,dwNum);  //将ASCII码转换成UTF-8
    
    httpsource=pwtext;
    
    
    return   httpsource;   //返回utf8格式http数据
    delete   pFile;           
    delete   conn1;           
    session1.Close();   
    

    }

    void CHttpClient::addParam(CString name, CString value)

    {

    names.AddTail((LPCTSTR)name);

    values.AddTail((LPCTSTR)value);

    CString eq="=";

    CString an="&";

    CONTENT=CONTENT+name+eq+value+an;

    CL=CONTENT.GetLength();

    }

    int main(void)
    {

    CHttpClient http;
    CString data="沈雪冰";
    
    
    wchar_t *pwtext=NULL;
    DWORD num=MultiByteToWideChar(CP_ACP,0,data,-1,NULL,0);
    pwtext=new wchar_t[num];
    MultiByteToWideChar(CP_UTF8,0,data,-1,pwtext,num);
    data=pwtext;
    
    
    CString result;
    http.addParam("data",data);
    result=http.doPost("http://192.168.0.115:8080/Struts2hello/helloworld.action");
    printf("%s\n",result);
    getchar();
    
    return 0;
    

    }

    点赞 1 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 2017-12-01 08:49

    后台,以及前台编码都要设置成UTF-8.

    点赞 评论 复制链接分享