Alicloud SMS interface

First of all, I have a rookie.
I just studied a few days ago. For wechat payment of small programs, the background is based on ssm. This time, experience the SMS notification after opening and submitting orders. So I took a look at Alibaba cloud.
First, open the SMS service. After entering the Alibaba cloud console, search for SMS. You can find it and create the AccessKey. Get AccessKey ID and AccessKey Secret.
next. Create a signature (the signed string after sending a short message). This should be separated from the encrypted signature and should not be confused. This signature should pay attention to the creation type, common, and verification code. It is generally common. The verification code is to verify identity and log in.. Wait, more important data. For this general message, you should choose GM, otherwise there will be ups and downs in sending messages later.
Next, create a SMS template. A paragraph with ${name} ${time} ${content} in the middle. One or more templates such as.
The above points are parameters when publishing SMS.
https://help.aliyun.com/document_detail/57534.html
Then go directly to the development guide, because a rookie won't look at the api and sdk. I tried. So neat, No. The following is the url submission to complete the SMS sending and feedback sending results.
The above steps and parameters are required.
Give me another quick start
https://help.aliyun.com/document_detail/101343.html
Enter this page. API reference - public information - Request signature. Yes, here's the signature again. This is a real signature. It needs to be checked. Similar to the payment signature of wechat applet, it takes two times, this time. That's POST with xml or json. This is GET with xml or json.
On this page, turn down and find the example direct copy. Put in the previously registered id and key. That's it. There may be a deep barrier. return new sun.misc.BASE64Encoder().encode(signData); The last line of code. Replace it with return Base64.getEncoder().encodeToString(signData); Simple and practical. get instant results.
then. Or send and return in the background. Use httpclient. Get method. See code for header format

	        String signurl = "http://dysmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp;
	    	String xml = "";

	     	xml = MapToXml.mapToXml(sortParas);		
	    	// Get the Http client (it can be understood that you have to have a browser first; note: in fact, the Http client is different from the browser)
 			CloseableHttpClient httpClient = HttpClientBuilder.create().build();
 	 
 			// Create Get request
 			HttpGet httpGet = new HttpGet(signurl);  
 			StringEntity entity = new StringEntity(xml, null, "UTF-8", false);
 	 
 			// The get request passes the parameters in the request body; Here, the entity is placed in the post request body
 			httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");
 			httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
 			httpGet.setHeader("Accept", "application/json;charset=utf8");
 			httpGet.setEntity(entity);
 	 
 			// Response model
 			CloseableHttpResponse response2 = null;
 			Map<String, String>  remap = new HashMap<String,String>();
 			try {
 				// The Post request is executed (sent) by the client
 				response2 = httpClient.execute(httpGet);
 				// Get response entity from response model
 				HttpEntity responseEntity = response2.getEntity();

 				if (responseEntity != null) {
 					System.out.println("Response content length is:" + responseEntity.getContentLength());
 					System.out.println(EntityUtils.toString(responseEntity));
 					//remap = XmlToMap.getXmlBodyContext(EntityUtils.toString(responseEntity));
 				}
 			} catch (ClientProtocolException e) {
 				e.printStackTrace();
 			} catch (IOException e) {
 				e.printStackTrace();
 			} finally {
 				try {
 					// Release resources
 					if (httpClient != null) {
 						httpClient.close();
 					}
 					if (response2 != null) {
 						response2.close();
 					}
 				} catch (IOException e) {
 					e.printStackTrace();
 				}
 			}

Keywords: Java cloud computing Alibaba Cloud

Added by Tomcat13 on Sat, 30 Oct 2021 23:12:38 +0300