package servlet; import com.alibaba.fastjson.JSON; import net.sf.json.JSONObject; import org.apache.http.Consts; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Set; public class WXPhoneServlet2 extends HttpServlet { String js_code; PrintWriter out; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); js_code = request.getParameter("code"); out = response.getWriter(); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=yourtooken"; Map<String, String> map = new HashMap<>(1); map.put("code", js_code); String res = WXPhoneServlet2.postJson(url, JSON.toJSONString(map), null); out.write("" + res); } public static String postJson(String url, String json, Map<String, String> headMap) { String returnStr; CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; try { HttpPost post = new HttpPost(url); post.setConfig(RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build()); httpClient = HttpClientBuilder.create().build(); StringEntity s = new StringEntity(json, Consts.UTF_8); s.setContentType("application/json"); if (headMap != null && headMap.size() > 0) { Set<String> keySet = headMap.keySet(); for (String key : keySet) { post.addHeader(key, headMap.get(key)); } } post.setEntity(s); httpResponse = httpClient.execute(post); BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name())); StringBuilder stringBuffer = new StringBuilder(100); String str; while ((str = reader.readLine()) != null) { stringBuffer.append(str); } returnStr = stringBuffer.toString(); reader.close(); return returnStr; } catch (SocketTimeoutException e) { return ""; } catch (ConnectTimeoutException e) { return ""; } catch (Exception e) { return ""; } finally { HttpClientUtils.closeQuietly(httpResponse); HttpClientUtils.closeQuietly(httpClient); } } }
phonenumber.getPhoneNumber接口问题https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html 请求地址:POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN [图片]
2022-04-14