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

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 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀