- Public static void Login () {
- String url = "Http://www.***.com/login";
- Postmethod Postmethod = new Postmethod (URL);
- //Fill in the values of each form field
- namevaluepair[] data = {
- New Namevaluepair ("account", "[email protected]"),
- New Namevaluepair ("Nexturl", " " "),
- New Namevaluepair ("Lcallback", " " "),
- New Namevaluepair ("password", "******"),
- New Namevaluepair ("Persistent", "1"),};
- //Put the value of the form into Postmethod
- Postmethod.setrequestbody (data);
- //Execute Postmethod
- int statusCode = 0;
- try {
- StatusCode = Httpclient.executemethod (Postmethod);
- } catch (HttpException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- //HttpClient for requests to receive subsequent services, such as post and put cannot automatically process forwarding
- //301 or 302
- if (StatusCode = = httpstatus.sc_moved_permanently
- || StatusCode = = httpstatus.sc_moved_temporarily) {
- //Remove the turning address from the head
- Header Locationheader = Postmethod.getresponseheader ("location");
- String location = null;
- if (locationheader! = null) {
- Location = Locationheader.getvalue ();
- System.out.println ("Diandianlogin:" + location);
- } Else {
- System.err.println ("Location field, value is null.");
- }
- return;
- } Else {
- System.out.println (Postmethod.getstatusline ());
- String str = "";
- try {
- str = postmethod.getresponsebodyasstring ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- System.out.println (str);
- }
- Postmethod.releaseconnection ();
- return;
- }
- One of the jar packages required:
- 1, Commons-httpclient.jar
- 2, Commons-codec.jar
- 3, Commons-logging.jar
Today, when developing, encountered the use of Java HttpClient class to post data, the target received after the Chinese garbled problem.
Request-Side code:
Java code
- /**
- * HttpClient Submission Parameters
- * @author [email protected]
- */
- Public static void Main (string[] args) throws IOException {
- HttpClient client = new HttpClient ();
- Client.gethostconfiguration (). Sethost ("127.0.0.1", 8081, "http");
- //Submit data using Post
- HttpMethod method = Getpostmethod ();
- Client.executemethod (method);
- //The status returned by the print server
- System.out.println (Method.getstatusline ());
- //Print results page
- String response = new String (Method.getresponsebodyasstring (). GetBytes ("8859_1"));
- //Print the returned information
- SYSTEM.OUT.PRINTLN (response);
- Method.releaseconnection ();
- }
- Submit data using Post
- Private static HttpMethod Getpostmethod () {
- String url = "/pushserver/notification.do?action=sendonemsg";
- Namevaluepair message = new Namevaluepair ("message", "messaging content.") ");
- Post.setrequestbody (new Namevaluepair[]{message});
- return post;
- }
- Submit data using Get method
- Private static HttpMethod Getgetmethod () {
- return new GetMethod ("/PUSHSERVER/NOTIFICATION.DO?ACTION=SENDONEMSG&MESSAGE=ABCD");
- }
Target-side code:
Java code
- /**
- * For msgserver Remote Call
- * @param request
- * @param response
- * @return
- * @throws Exception
- * @author [email protected]
- */
- Public Modelandview sendonemsg (HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- String message = Servletrequestutils.getstringparameter (Request, "message");
- }
After this code executes, the target can receive the message, but the Chinese garbled, also did not find the Transcoding method.
After analysis, the parameters of the HTTP request that were originally added with Namevaluepair will eventually be converted to requestentity submission to the HTTP server. Then, in Postmethod's parent class, Entityenclosingmethod found that the encoding (character set) of the commit can be set as long as the Getrequestcharset () method is overloaded.
After correction:
Java code
- /**
- * HttpClient Submission Parameters
- * @author [email protected]
- */
- Public static void Main (string[] args) throws IOException {
- HttpClient client = new HttpClient ();
- Client.gethostconfiguration (). Sethost ("127.0.0.1", 8081, "http");
- //Submit data using Post
- HttpMethod method = Getpostmethod ();
- Client.executemethod (method);
- //The status returned by the print server
- System.out.println (Method.getstatusline ());
- //Print results page
- String response = new String (Method.getresponsebodyasstring (). GetBytes ("8859_1"));
- //Print the returned information
- SYSTEM.OUT.PRINTLN (response);
- Method.releaseconnection ();
- }
- Submit data using Post
- Private HttpMethod Getpostmethod () {
- String url = "/pushserver/notification.do?action=sendonemsg";
- Postmethod post = new Utf8postmethod (URL);
- Namevaluepair message = new Namevaluepair ("message", "messaging content.") ");
- Post.setrequestbody (new Namevaluepair[]{message});
- return post;
- }
- Inner Class for UTF-8 support
- Public static class Utf8postmethod extends postmethod{
- Public utf8postmethod (String url) {
- super (URL);
- }
- @Override
- Public String Getrequestcharset () {
- //return Super.getrequestcharset ();
- return "UTF-8";
- }
- }
- Submit data using Get method
- Private static HttpMethod Getgetmethod () {
- return new GetMethod ("/PUSHSERVER/NOTIFICATION.DO?ACTION=SENDONEMSG&MESSAGE=ABCD");
- }
Java analog Post form submit httpclient action