HTTP request, HttpClient, call SMS interface

Source: Internet
Author: User

Security settings in the project to retrieve the password function, you need to send a text message to verify the binding phone, through the binding phone number verification and reset password.

Because the project is managed through MAVEN, the jar package needs to be introduced in the Pom.xml file.

The jar package introduced by Maven:

        <!--https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -        <Dependency>            <groupId>Commons-httpclient</groupId>            <Artifactid>Commons-httpclient</Artifactid>            <version>3.1</version>        </Dependency>

It is important to note that, because of the version compatibility and other reasons, the introduction is not the latest 3.1 version, but rather the older version of the 2.*, this version does not set the character type method, so only the default character type to send data, the result returns fail, Later testing in the absence of Chinese character judgment can return to OK, and through postman view:

As a result, the problem of sending the Chinese characters in the request is determined, and later modified to the latest 3.1 version after the format string "UTF-8", can send the request and return true correctly.

Part of the code that sent the request:

String info =NULL; Try{HttpClient HttpClient=NewHttpClient ();//Method InvocationPostmethod Post=NewPostmethod ("Http://192.168.1.99:8088/sms/sendCode");//Interface Addresspost.getparams (). Setparameter (Httpmethodparams.http_content_charset,"UTF-8");//Format ConversionPost.addparameter ("Signname", "Zibo * * smart"); Post.addparameter ("Templatecode", "sms_114600059"); Post.addparameter ("Expiretime", "300"); Post.addparameter ("Count", "4"); Post.addparameter ("Bizid", "Learn"); Post.addparameter ("Phonenumbers", List.get (0). Getphone ());                  Httpclient.executemethod (POST); Info=NewString (Post.getresponsebody (), "UTF-8"); Mav.addobject ("Apimsg", info); }Catch(Exception e) {e.printstacktrace (); }  

The returned OK sends the request successfully,

The returned later sent over please later,

The return of fail, which was not set before the character encoding type is just returned,

There are also return supervisor errors due to incorrect parameter names, and so on.

The user enters the verification code to determine if it is correct:

New HttpClient (); // method invocation              New Postmethod ("Http://192.168.1.99:8088/sms/validCode"); // Interface Address               // character encoding is not set here, and can also be passed because there is no kanji            Post.addparameter ("Bizid", "learn");            Post.addparameter ("code", Pnum);            Post.addparameter ("PhoneNumber", "17560306966");            Httpclient.executemethod (post);                          New

This is based on the input verification code to determine whether the correct, return OK for the input of the verification code and send the same verification code.

HTTP request, HttpClient, call SMS interface

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.