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

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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?