package com.kidgrow.sms.util; import com.alibaba.fastjson.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class SmsChuangLanUtils { public static void main(String[] args) { //短信下发 String sendUrl = "http://smssh1.253.com/msg/send/json"; Map map = new HashMap(); map.put("account","N2561124");//API账号 map.put("password","Mguj6qlRWX7b5e");//API密码 map.put("msg","123123");//短信内容 map.put("phone","18600376209");//手机号 map.put("report","false");//是否需要状态报告 JSONObject js = (JSONObject) JSONObject.toJSON(map); System.out.println(sendSmsByPost(sendUrl,js.toString())); } public static String send(Map map,String url){ JSONObject js = (JSONObject) JSONObject.toJSON(map); return sendSmsByPost(url,js.toString()); } public static String sendSmsByPost(String path, String postContent) { URL url = null; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.connect(); OutputStream os=httpURLConnection.getOutputStream(); os.write(postContent.getBytes("UTF-8")); os.flush(); StringBuilder sb = new StringBuilder(); int httpRspCode = httpURLConnection.getResponseCode(); if (httpRspCode == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream(), "utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); return sb.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } }