[Instance] JAVA calls the WeChat interface to send text and text messages. You do not need to jump to the details page.

Source: Internet
Author: User
Tags openid

[Instance] JAVA calls the interface to send text and text messages. You do not need to jump to the details page.

Package com. test; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import org. junit. test; import net. sf. json. JSONArray; import net. sf. json. JSONObject; public class WechatServlet {
// Get the returned access_token private String getAccess_token () {String access_token = null; StringBuffer action = new StringBuffer (); action. append ("https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential "). append ("& appid = ****************") // set the appid of the service number. append ("& secret = *********************************" ); // set the key URL of the service number; try {
// Simulate get request url = new URL (action. toString (); HttpURLConnection http = (HttpURLConnection) url. openConnection (); http. setRequestMethod ("GET"); http. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); http. setDoInput (true); InputStream is = http. getInputStream (); int size = is. available (); byte [] buf = new byte [size]; is. read (buf); String resp = new String (buf, "UTF-8"); JSONObject json = JSONObject. fromObject (resp); Object object = json. get ("access_token"); if (object! = Null) {access_token = String. valueOf (object);} System. out. println ("getAccess_token:" + access_token); return access_token;} catch (MalformedURLException e) {e. printStackTrace (); return access_token;} catch (IOException e) {e. printStackTrace (); return access_token;} // get the user group public JSONArray getOpenids () {JSONArray array = null; String urlstr = "https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ACCESS_TOKEN & next_openid = NEXT_OPENID "; urlstr = urlstr. replace ("ACCESS_TOKEN", getAccess_token (); urlstr = urlstr. replace ("NEXT_OPENID", ""); URL url; try {url = new URL (urlstr); HttpURLConnection http = (HttpURLConnection) url. openConnection (); http. setRequestMethod ("GET"); http. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); http. setDoInput (true); InputStream Is = http. getInputStream (); int size = is. available (); byte [] buf = new byte [size]; is. read (buf); String resp = new String (buf, "UTF-8"); JSONObject jsonObject = JSONObject. fromObject (resp); System. out. println ("getOpenids:" + jsonObject. toString (); array = jsonObject. getJSONObject ("data "). getJSONArray ("openid"); return array;} catch (MalformedURLException e) {e. printStackTrace (); return array;} catch (I OException e) {e. printStackTrace (); return array ;}// obtain user detailed data according to the user group's openId public JSONObject getUserOpenids (String openId) {String urlstr = "https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ACCESS_TOKEN & openid = OPENID & lang = zh_CN "; urlstr = urlstr. replace ("ACCESS_TOKEN", getAccess_token (); urlstr = urlstr. replace ("OPENID", openId); urlstr = urlstr. replace ("NEXT_OPENID", ""); URL url; try {url = new URL (urlstr); HttpURLConnection http = (HttpURLConnection) url. openConnection (); http. setRequestMethod ("GET"); http. setRequestProperty ("Content-Type", "application/x-www-form-urlenc Oded "); http. setDoInput (true); InputStream is = http. getInputStream (); int size = is. available (); byte [] buf = new byte [size]; is. read (buf); String resp = new String (buf, "UTF-8"); JSONObject jsonObject = JSONObject. fromObject (resp); System. out. println ("getUserOpenids:" + jsonObject. toString (); return jsonObject;} catch (MalformedURLException e) {e. printStackTrace (); return null;} catch (IOException e) {E. printStackTrace (); return null ;}// main method. Only the service number can use the customer service text message. Click the text link to directly jump to the specified URL @ Test public void testsendTextByOpenids () {// String urlstr = "https://api.weixin.qq.com/cgi-bin/message/mass/send? Access_token = ACCESS_TOKEN "; // group text message String urlstr =" https://api.weixin.qq.com/cgi-bin/message/custom/send? Access_token = ACCESS_TOKEN "; // send the customer service graphic message urlstr = urlstr. replace ("ACCESS_TOKEN", token (); String reqjson = createGroupText (getOpenids (); try {URL httpclient = new URL (urlstr); HttpURLConnection conn = (HttpURLConnection) httpclient. openConnection (); conn. setConnectTimeout (5000); conn. setreadtimeouts (2000); conn. setRequestMethod ("POST"); conn. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); conn. setDoOutput (true); conn. setDoInput (true); conn. connect (); OutputStream OS = conn. getOutputStream (); System. out. println ("ccccc:" + reqjson); OS. write (reqjson. getBytes ("UTF-8"); // input parameter OS. flush (); OS. close (); InputStream is = conn. getInputStream (); int size = is. available (); byte [] jsonBytes = new byte [size]; is. read (jsonBytes); String message = new String (jsonBytes, "UTF-8"); System. out. println ("testsendTextByOpenids:" + message);} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}// create the sent data private String createGroupText (JSONArray array) {JSONObject gjson = new JSONObject (); // JSONObject json = getUserOpenids (array. get (3 ). toString (); // The array parameter is used by all users of the user group. This method prints the detailed information of one of the users in array gjson. put ("touser", array. get (3); gjson. put ("msgtype", "news"); JSONObject news = new JSONObject (); JSONArray articles = new JSONArray (); JSONObject list = new JSONObject (); list. put ("title", "title"); // title list. put ("description", "description"); // description list. put ("url", "http: //"); // click the url list to jump. put ("picurl", "http: //"); // The image articles of the Graphic Link. add (list); news. put ("articles", articles); JSONObject text = new JSONObject (); gjson. put ("text", text); gjson. put ("news", news); return gjson. toString ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.