顾空 2013-11-26 07:41
浏览 5941

c++ post提交数据中文乱码

vc6.0 mfc
客户端用post提交中文数据,在web服务端接收是乱码,初步分析是编码问题,如果将中文转换成utf-8发送,则服务端能正常接收中文。

测试时我是借用的浏览器将中文转换成utf-8的,然后用转换后的字符串直接提换中文。
比如:

一:PostHttpPage("0.citygo.duapp.com","orderfrom_new","clientName=中文");
二:PostHttpPage("0.citygo.duapp.com","orderfrom_new","clientName=%E4%B8%AD%E6%96%87");

PostHttpPage函数借用其他人的,代码如下:

bool PostHttpPage(const std::string& hostName, const std::string& pathName, const std::string& postData)
{
using namespace std;

CInternetSession session("your");

try
{
    INTERNET_PORT nPort = 80;
    DWORD dwRet = 0;

    CHttpConnection* pServer = session.GetHttpConnection(hostName.c_str(), nPort);
    CHttpFile* pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, pathName.c_str());

    CString strHeaders = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"; // 请求头

    //开始发送请求

    pFile->SendRequest(strHeaders,(LPVOID)postData.c_str(),postData.size());
    pFile->QueryInfoStatusCode(dwRet);


    if (dwRet == HTTP_STATUS_OK)
    {
        CString result, newline;

        while(pFile->ReadString(newline))
        {//循环读取每行内容
            result += newline+"\r\n";
        }

ofstream fout;
fout.open("output.txt");
fout<<result;
fout.close();
    }
    else
    {
        return false;
    }
    delete pFile;
    delete pServer;

}
catch (CInternetException* pEx)
{
    //catch errors from WinInet
    TCHAR pszError[200];
    pEx->GetErrorMessage(pszError, 200);

    std::cout<<pszError<<std::endl;//显示异常信息
    return false;
}
session.Close();

return true;

}

服务端代码如下:

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
     String rel=request.getParameter("clientName").toString();
        out.write(rel);
    }
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建