Initiate post requests and Java Servlet responses with Delphi's Tidhttp control

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.