问题描述
- 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+"rn"; } 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); }
时间: 2024-11-03 17:02:32