1.5 调用实例Java版本

1. 使用HttpClient    依赖:Apache HttpClient + Jackson Lib

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.5.3</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.3</version>
</dependency>
 package xx.demo;

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;
/**
* a demo for how to call xx sms smsapi, more protocol detail refer to :
* http://www.17int.cn/xxAdmin/xxhttpProto.htm
* @author SamLin
*/
public class SMSSender { public static String requestUrl = "http://www.17int.cn/xxsmsweb/smsapi/send.json"; public static String encoding = "UTF-8"; public static void main(String[] args) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(requestUrl); SMSContent smsDetail = new SMSContent(); smsDetail.setAccount("ACCOUNT"); smsDetail.setPassword("MD5ENCODED_PASS"); // MD5 UP case encoding password smsDetail.setContent("【享迅】你的验证码是112233, 20分钟有效。"); smsDetail.setExtno("");// max length for 3, by default treats as empty smsDetail.setRequestId(String.valueOf(System.nanoTime())); smsDetail.setMobile("135xxxxxxxx"); 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(); } } } } public class SMSContent { private String account; private String password; private String mobile; private String content; private String requestId; 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 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; } public String getRequestId() { return requestId; } public void setRequestId(String requestId) { this.requestId = requestId; } public String getExtno() { return extno; } public void setExtno(String extno) { this.extno = extno; } }
Out put:

Get Response content:
    {
        "requestId":"676418101578191",
        "status":"10",
        "batchId":"2686825",
        "errorCode":"ALLSuccess"
    }
                            
2. 使用URLConnection    依赖: Jackson Lib (maven resp 参考上面)
package xx.demo;


import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import org.junit.Test;

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

import com.fasterxml.jackson.databind.ObjectMapper;


public class UrlConnectionSender {

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

    public static String encoding = "UTF-8";

    @Test

    public void test() throws MalformedURLException{

        URL callURL=new URL(requestUrl);

        SMSContent smsDetail = new SMSContent();

        smsDetail.setAccount("yourAccount");

        smsDetail.setPassword("yourPassWithMD5UpperCase");//MD5Encoded

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

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

        smsDetail.setRequestId(String.valueOf(System.nanoTime()));

        smsDetail.setMobile("13544354411");

        ObjectMapper mapper = new ObjectMapper();

        mapper.setSerializationInclusion(Include.NON_NULL);

        // use jackson to format as json.

        String jsonContent = "";

        try{

            jsonContent = mapper.writeValueAsString(smsDetail);

            HttpURLConnection conn = (HttpURLConnection) callURL.openConnection();

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Accept", "application/json");

            conn.setRequestProperty("connection", "Keep-Alive");

            conn.setRequestProperty("Charsert", "UTF-8");

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

            conn.setd class="breakLine"oInput(true);

            conn.setd class="breakLine"oOutput(true);

            conn.setInstanceFollowRedirects(true);

            conn.connect();

            OutputStream os = conn.getOutputStream();

            PrintWriter out = new PrintWriter(os);

            out.print(jsonContent);

            out.flush();

            os.flush();

            out.close();

            os.close();

            InputStream is = conn.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            String s = br.readLine();

            System.out.println(s);

            br.close();

            isr.close();

            is.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}