java实现微信公众平台自定义菜单的创建示例

时间:2021-05-19

复制代码 代码如下:
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.json.JSONObject;

public class MenuUtil {

/**
* 获得ACCESS_TOKEN
* @Title: getAccess_token
* @Description: 获得ACCESS_TOKEN
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
private static String getAccess_token(){

String APPID="";
String APPSECRET="";

String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" +APPSECRET;
String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();

http.setRequestMethod("GET"); //必须是get方式请求
http.setRequestProperty("Content-Type","application/x-/cgi-bin/menu/delete? access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-.client.defaultReadTimeout", "30000"); //读取超时30秒

http.connect();
OutputStream os= http.getOutputStream();
os.flush();
os.close();

InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "deleteMenu返回信息:"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "deleteMenu 失败";
}
public static void main(String[] args) {

System.out.println(createMenu());
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章