当前位置: 首页 > 后端技术 > Java

短信平台API接口演示示例-JAVA-Message-Send

时间:2023-04-01 18:06:46 Java

DEMO:Message/Send原始链接支持JDK版本:1.5以上依赖的jar包:httpclient-4.5.3.jar、httpcore-4.4.14.jar、commons-logging1.1.1.jar、fastjson-1.2.75.jar<依赖项>org.apache.httpcomponentshttpclient4.5.3org.apache.httpcomponentshttpcore4.4.14commons-loggingcommons-logging<版本>1.1.1com.alibabafastjson1.2.75代码提示列表MessageSendDemopackagecom.submail.demo.message;导入java.io.IOException;导入java.util.TreeMap;导入com.alibaba.fastjson.JSONObject;导入com.submail.demo.RequestEncoder;导入org.apache.http.HttpEntity;导入org.apache.http.HttpResponse;导入org.apache.http.client.ClientProtocolException;导入org.apache.http.client.methods.HttpGet;导入org.apache.http.client.methods.HttpPost;导入org.apache.http.entity.StringEntity;导入org.apache.http.impl.client.CloseableHttpClient;导入org.apache.http.impl.client.HttpClientBuilder;导入org.apache.http.util.EntityUtils;公共类MessageSendDemo{publicstaticfinalStringTIMESTAMP="https://api.mysubmail.com/service/timestamp";privatestaticfinalStringURL="https://api.mysubmail.com/message/send";publicstaticfinalStringTYPE_MD5="md5";publicstaticfinalStringTYPE_SHA1="sha1";publicstaticvoidmain(String[]args){TreeMaprequestData=newTreeMap();字符串appid="";字符串appkey="";字符串=“176xxxx5149”;Stringcontent="【xxxx】你好,你的验证码是2257";字符串sign_type="md5";字符串sign_version="2";//组合请求数据requestData.put("appid",appid);requestData.put("到",到);如果(sign_type.equals(TYPE_MD5)||sign_type.equals(TYPE_SHA1)){Stringtimestamp=getTimestamp();requestData.put("时间戳",时间戳);requestData.put("sign_type",sign_type);requestData.put("sign_version",sign_version);StringsignStr=appid+appkey+RequestEncoder.formatRequest(requestData)+appid+appkey;System.out.println(signStr);requestData.put("signature",RequestEncoder.encode(sign_type,signStr));}else{requestData.put(“签名”,应用程序密钥);}requestData.put("内容",内容);//post请求HttpPosthttpPost=newHttpPost(URL);httpPost.setHeader("接受","应用程序/json");httpPost.setHeader("Content-Type","application/json");StringEntityentity=newStringEntity(JSONObject.toJSONString(requestData),"UTF-8");httpPost.setEntity(实体);尝试{CloseableHttpClientcloseableHttpClient=HttpClientBuilder.create().build();HttpResponse响应=closeableHttpClient.execute(httpPost);HttpEntityhttpEntity=response.getEntity();如果(httpEntity!=null){StringjsonStr=EntityUtils.toString(httpEntity,"UTF-8");System.out.println(jsonStr);}}catch(ClientProtocolExceptione){e.printStackTrace();}抓住(IOExceptione){e.printStackTrace();}}//获取时间戳privatestaticStringgetTimestamp(){CloseableHttpClientcloseableHttpClient=HttpClientBuilder.create().build();HttpGethttpget=newHttpGet(TIMESTAMP);尝试{HttpResponseresponse=closeableHttpClient.execute(httpget);HttpEntityhttpEntity=response.getEntity();字符串jsonStr=EntityUtils.toString(httpEntity,"UTF-8");if(jsonStr!=null){JSONObjectjson=JSONObject.parseObject(jsonStr);返回json.getString("时间戳");}}catch(ClientProtocolExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}返回空值;}}RequestEncoderpackagecom.submail.demo;/***@authorzhang*@date2021/8/4-5:52下午*/importjava.security.MessageDigest;importjava.util.Iterator;importjava.util.Map;importjava.util.Set;publicclassRequestEncoder{privatestaticfinalchar[]HEX_DIGITS={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};publicstaticStringencode(Stringalgorithm,Stringstr){if(str==null){返回null;}try{MessageDigestmessageDigest=MessageDigest.getInstance(algorithm);messageDigest.update(str.getBytes("UTF-8"));返回getFormattedText(messageDigest.digest());}catch(Exceptione){thrownewRuntimeException(e);}}privatestaticStringgetFormattedText(byte[]bytes){intlen=bytes.length;StringBuilderbuf=newStringBuilder(len*2);对于(intj=0;j>4)&0x0f]);buf.append(HEX_DIGITS[字节[j]&0x0f]);}返回buf.toString();}publicstaticStringformatRequest(Mapdata){SetkeySet=data.keySet();Iteratorit=keySet.iterator();StringBuffersb=newStringBuffer();while(it.hasNext()){字符串键=it.next();对象值=data.get(key);if(valueinstanceofString){sb.append(key+"="+value+"&");}}if(sb.length()!=0){System.out.println("sb.substring(0,sb.length()-1)="+sb.substring(0,sb.length()-1));返回sb.substring(0,sb.length()-1);}返回空值;}}

最新推荐
猜你喜欢