Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.abc.turkey.service.unfreeze; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.abc.turkey.service.ServiceException; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; /** * sms?. * * @author calvin */ @Component public class SmsService { private static Logger logger = LoggerFactory.getLogger(SmsService.class); private final String loginUrl = "http://www.7tds.com/c/user_login"; private final String releaseUrl = "http://www.7tds.com/api/order/releaseall"; private final String fetchUrl = "http://www.7tds.com/api/order/getphoneofsend?&numbers=3&pid=33WXXZWP6LYN&cid=sendforcode"; // ? private List<String> phoneList = null; // ?did private List<String> didList = null; // ??? private int numberIndex = 0; private CloseableHttpClient httpclient = HttpClients.createDefault(); private void login() { try { HttpPost httpPost = new HttpPost(loginUrl); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "yuzhibo468")); nvps.add(new BasicNameValuePair("password", "qq123654")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpclient.execute(httpPost); String res = EntityUtils.toString(response.getEntity(), "utf8"); logger.debug(res); response.close(); } catch (Exception e) { throw new ServiceException("Sms api login error!"); } } private String checkRes(String res, HttpUriRequest lastRequest) throws Exception { String errorCode = JSON.parseObject(res).getString("error"); if (errorCode == null) { return res; } if (errorCode.equals("10001")) { // ,,?? login(); CloseableHttpResponse response = httpclient.execute(lastRequest); res = EntityUtils.toString(response.getEntity(), "utf8"); logger.debug(res); response.close(); } else { // throw new ServiceException("Sms api error, code: " + errorCode); } return res; } private synchronized String getNextDid() throws Exception { int posInList = numberIndex / 3; // ????3 if (didList != null && posInList < didList.size()) { numberIndex++; return didList.get(posInList); } if (phoneList == null) { // ?? HttpGet httpGet = new HttpGet(releaseUrl); CloseableHttpResponse response = httpclient.execute(httpGet); String res = EntityUtils.toString(response.getEntity(), "utf8"); response.close(); logger.debug(res); checkRes(res, httpGet); } else { // ?? String blackUrl = "http://www.7tds.com/api/order/toblacklist?pid=33WXXZWP6LYN&phones="; for (int i = 0; i < phoneList.size(); i++) { if (i == 0) { blackUrl += phoneList.get(i); } else { blackUrl += (',' + phoneList.get(i)); } } HttpGet httpGet = new HttpGet(blackUrl); CloseableHttpResponse response = httpclient.execute(httpGet); String res = EntityUtils.toString(response.getEntity(), "utf8"); response.close(); logger.debug("to blackList res:" + res); checkRes(res, httpGet); } // ?oid HttpGet httpGet = new HttpGet(fetchUrl); CloseableHttpResponse response = httpclient.execute(httpGet); String res = EntityUtils.toString(response.getEntity(), "utf8"); response.close(); res = checkRes(res, httpGet); String orderId = JSON.parseObject(res).getString("order_id"); // ?did httpGet = new HttpGet("http://www.7tds.com/api/order/result?&oid=" + orderId); response = httpclient.execute(httpGet); res = EntityUtils.toString(response.getEntity(), "utf8"); response.close(); res = checkRes(res, httpGet); logger.debug(res); JSONArray array = JSON.parseObject(res).getJSONArray("rows"); didList = new ArrayList<String>(); for (int i = 0; i < array.size(); i++) { didList.add(array.getJSONObject(i).getString("order_detail_id")); } // ? numberIndex = 1; return didList.get(0); } public void sendSms(String smsCode) throws Exception { String did = getNextDid(); String content = "{\"code\":\"" + smsCode + "\"}"; content = URLEncoder.encode(content, "utf-8"); String sendUrl = "http://www.7tds.com/api/order/send?&did=" + did + "&inputs=" + content; HttpGet httpGet = new HttpGet(sendUrl); CloseableHttpResponse response = httpclient.execute(httpGet); String res = EntityUtils.toString(response.getEntity(), "utf8"); response.close(); logger.debug(res); checkRes(res, httpGet); } }