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

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 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)