2.5 调用实例Java版本

依赖:Apache HttpClient + Jackson Lib (依赖包参考1.5)

import java.nio.charset.Charset;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;

import com.fasterxml.jackson.databind.ObjectMapper;

public class BatchSMSSender {

    public static String requestUrl = "http://www.17int.cn/xxsmsweb/smsapi/batchSend.json";

    public static String encoding = "UTF-8";

    public static void main(String[] args) {

        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpPost httppost = new HttpPost(requestUrl);

        BatchSMSContent smsDetail = new BatchSMSContent();

        smsDetail.setAccount("samgroup");

        smsDetail.setPassword("E99A18C428CB38D5F260853678922E03");

        smsDetail.setExtno("123");// max length for 3, by default treats as empty


        BatchRequestContent content1 = new BatchRequestContent();

        content1.setContent("【享迅】你的验证码是112233, 20分钟有效。");

        content1.setMobile("13544444444,13729999999");

        BatchRequestContent content2 = new BatchRequestContent();

        content2.setContent("【享迅】你的验证码是112244, 20分钟有效。");

        content2.setMobile("13826666666");

        BatchRequestContent[] bReqContents = new BatchRequestContent[]{
            content1,content2
        };

        smsDetail.setbReqContents(bReqContents);

        ObjectMapper mapper = new ObjectMapper();

        mapper.setSerializationInclusion(Include.NON_NULL);

        // use jackson to format as json.

        String jsonContent = "";

        String respContent = "";

        try {
            jsonContent = mapper.writeValueAsString(smsDetail);

            httppost.addHeader("Content-type", "application/json; charset=utf-8");

            httppost.setHeader("Accept", "application/json");

            httppost.setEntity(new StringEntity(jsonContent, Charset.forName(encoding)));

            CloseableHttpResponse response = httpclient.execute(httppost);

            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    respContent = EntityUtils.toString(entity, encoding);

                    System.out.println("Get Response content:" + respContent);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            System.out.println(" Not able to get response correctly. get error " + e);
        } finally {
            try {
                httpclient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


package xx.demo;

public class BatchSMSContent {

    private String account;

    private String password;

    private BatchRequestContent[] bReqContents;

    private String extno;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public BatchRequestContent[] getbReqContents() {
        return bReqContents;
    }

    public void setbReqContents(BatchRequestContent[] bReqContents) {
        this.bReqContents = bReqContents;
    }

    public String getExtno() {
        return extno;
    }

    public void setExtno(String extno) {
        this.extno = extno;
    }
}

package xx.demo;

public class BatchRequestContent {

    private String mobile;

    private String content;

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}