Solve the problem of Chinese garbled characters in delphi client using TIDhttp POST

Recently, I am using delphi to write the client, and use the Tidhttp post method to pass parameters to the server, but I encountered the problem of Chinese garbled characters.
1, Delphi pass Chinese to Java is garbled.
Above code:
procedure TForm18.Button1Click(Sender: TObject);
var
  userName : String ;
  userPwd : String ;
  Url: String ;
  Params : TStringList ;
  result : string ;
begin
  if edtUserName.Text ='' then
    showmessage('Please enter the username ')
    else if edtPwd.Text ='' then
    showmessage('Please enter your password')
    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");就解决了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041079&siteId=291194637