Using the Delphi Tidhttp control to initiate a POST request is simple, define a tstringlist instance, add parameters to the above line:
Procedure Tform1.button1click (Sender:tobject);
Var
Httpclient:tidhttp;
Paramlist:tstringlist;
Begin
HttpClient: = Tidhttp.create ();
Paramlist: = tstringlist.create;
Paramlist.add (' User=pan ');
ShowMessage (Httpclient.post (' Http://192.168.0.105:8080/Servlets/Submit ', paramlist));
End
Run the code and use the Wireshark grab tool to see this post request coming out, Content-type is application/x-www-form-urlencoded, this type means to package the parameters into Key-value form.
Then take a look at the server's Java code:
public class Submit extends HttpServlet {
...
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Response.getwriter (). println ("Hello Servlet," + request.getparameter ("user") + "!");
}
}
When the client receives a post response, the "Hello Servlet, pan!" This text shows up.
Add:
When a Post's parameter value is in Chinese, it is encoded as a UTF8 format, such as a wireshark grab for the Post method:
And look at the grab bag of OK messages:
At this point can not directly use the Tidhttp.post method to return the string, but to its correct decoding, otherwise read is garbled:
Procedure Tform1.button1click (sender:tobject);
var
httpclient:tidhttp;
Paramlist:tstringlist;
Ss:tstringstream;
Begin
SS: = Tstringstream.create (', Tencoding.utf8);
HttpClient: = Tidhttp.create ();
Paramlist: = tstringlist.create;
Paramlist.add (' action= test ');
Paramlist.add (' user= user ');
Httpclient.post (' Http://192.168.0.102:8080/HelloServlet/Submit ', paramlist, SS);
ShowMessage (SS. datastring);
End
The way to solve the garbled problem is that we decode the returned stream of characters by Tstringstream by UTF8.
Supplement 2: When there are two identical parameter names in the Post method, for example:
Procedure Tform1.button1click (sender:tobject);
var
httpclient:tidhttp;
Paramlist:tstringlist;
Ss:tstringstream;
Begin
SS: = Tstringstream.create (', Tencoding.utf8);
HttpClient: = Tidhttp.create ();
Paramlist: = tstringlist.create;
Paramlist.add (' action= test ');
Paramlist.add (' user= user 1 ');
Paramlist.add (' user= user 2 ');
Httpclient.post (' Http://192.168.0.100:8080/HelloServlet/Submit ', paramlist, SS);
ShowMessage (SS. datastring);
End
At this point, if you still use the GetParameter method on the server side, you can only return the first value "User 1" and you need to use the Getparametervalues method:
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException c1/>//TODO auto-generated Method stub
// Response.getwriter (). println ("Hello Servlet," + Request.getparameter ("user") + "!");
string[] user = request.getparametervalues ("user");
Response.getwriter (). println ("Hello Servlet," + user[0] + "and" + user[1] + "!");
}
At this point the client receives:
Therefore, the two values of the parameter with the same name are read.