解决delphi客户端用TIDhttp POST 传参中文乱码问题
程序员文章站
2022-03-03 18:38:49
...
最近在用delphi写客户端,用Tidhttp post方法向服务器传参,但是碰到了中文乱码的问题。
1、delphi传中文到Java是乱码。
上代码:
procedure TForm18.Button1Click(Sender: TObject);
var
userName : String ;
userPwd : String ;
Url: String ;
Params : TStringList ;
result : string ;
begin
if edtUserName.Text ='' then
showmessage('请输入用户名')
else if edtPwd.Text ='' then
showmessage('请输入密码')
else
begin
userName := edtUserName.Text ;
userPwd := edtPwd.Text ;
Params := TStringList.Create ;
Params.Add('userName='+userName) ;
Params.Add('userPwd='+userPwd);
idHTTP1.Request.ContentType:='application/x-www-form-urlencoded; Charset=UTF-8';
Url := 'http://192.168.1.104:8080/myservlet/servlet/Hello';
result := IdHTTP1.Post(Url, Params);
end;
end;
只要加一句idHTTP1.Request.ContentType:='application/x-www-form-urlencoded; Charset=UTF-8';就可以解决。
2、Java传中文到delphi是乱码。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html:charset=utf_8");
response. setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
login(request ,response);
}
public void login (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html:charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
LoginDAO loginDAO = new LoginDAO();
List<Operator> list = loginDAO.searchUser(userName , userPwd );
}
加一句response.setCharacterEncoding("UTF-8");就解决了。
1、delphi传中文到Java是乱码。
上代码:
procedure TForm18.Button1Click(Sender: TObject);
var
userName : String ;
userPwd : String ;
Url: String ;
Params : TStringList ;
result : string ;
begin
if edtUserName.Text ='' then
showmessage('请输入用户名')
else if edtPwd.Text ='' then
showmessage('请输入密码')
else
begin
userName := edtUserName.Text ;
userPwd := edtPwd.Text ;
Params := TStringList.Create ;
Params.Add('userName='+userName) ;
Params.Add('userPwd='+userPwd);
idHTTP1.Request.ContentType:='application/x-www-form-urlencoded; Charset=UTF-8';
Url := 'http://192.168.1.104:8080/myservlet/servlet/Hello';
result := IdHTTP1.Post(Url, Params);
end;
end;
只要加一句idHTTP1.Request.ContentType:='application/x-www-form-urlencoded; Charset=UTF-8';就可以解决。
2、Java传中文到delphi是乱码。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html:charset=utf_8");
response. setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
login(request ,response);
}
public void login (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html:charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
LoginDAO loginDAO = new LoginDAO();
List<Operator> list = loginDAO.searchUser(userName , userPwd );
}
加一句response.setCharacterEncoding("UTF-8");就解决了。