1.前端传数据只需要传递code
post请求携带参数为code,请求中的access_token需要用APPID和密钥获取
2.后台代码
@ResponseBody
@ApiOperation(value = "获取小程序用户基本信息(手机号)", httpMethod = "GET")
@RequestMapping(value = "/code2Session", method = RequestMethod.GET)
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "code", dataType = "String", value = "微信的 code", required = true),
@ApiImplicitParam(paramType = "query", name = "openid", dataType = "String", value = "微信的 openid", required = true),
})
public CommonResponse code2Session(String code, String openid) throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret;
JSONObject jsonObject = HttpUtil.doGetJson(url);
log.info(jsonObject.getString("access_token"));
String url1 = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + jsonObject.getString("access_token");
log.info(jsonObject.toString());
JSONObject data = new JSONObject();
data.put("code", code);
JSONObject jsonObject1 = HttpUtil.DO_POST(url1,data);
log.info(jsonObject1.toString());
JSONObject phone_info = (JSONObject) jsonObject1.get("phone_info");
String phoneNumber = phone_info.getString("phoneNumber");
log.info(phoneNumber);
UserInfo user = userService.findByOpenID(openid);
if (user == null){
return CommonResponse.fail(201, "请先授权");
}
user.setPhonenumber(phoneNumber);
userService.upDate(user);
return CommonResponse.ok(phoneNumber);
}
3.HttpUtils
package com.school.zhaoban.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.school.zhaoban.response.CommonResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.*;
public class HttpUtil {
private static Certificate cert = null;
private static SSLConnectionSocketFactory socketFactory;
private static TrustManager manager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
private static void enableSSl(boolean isIgnore) {
try {
SSLContext context = SSLContext.getInstance("TLS");
if (isIgnore) {
context.init(null, new TrustManager[]{manager}, null);
} else {
context.init(null, new TrustManager[]{httpsManager}, null);
}
socketFactory = new SSLConnectionSocketFactory(context, NoopHostnameVerifier
.INSTANCE);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
private static void initHttpsCertificate(String certificateUrl) {
try {
FileInputStream fis = new FileInputStream(certificateUrl);
BufferedInputStream bis = new BufferedInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
cert = cf.generateCertificate(bis);
}
bis.close();
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
}
}
private static TrustManager httpsManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] { (X509Certificate) cert };
}
};
public static String Method(Map<String,String> map, String token,String creat_url) throws IOException {
URL restURL = new URL(creat_url);
JSONObject jsonObject = new JSONObject();
jsonObject.put("no",map.get("no"));
String s1 = jsonObject.toJSONString();
HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", token);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print(s1);
ps.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String str = null;
while((line = br.readLine()) != null ){
str = line;
}
br.close();
return str;
}
public static JSONObject DO_POST(String url, JSONObject json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());
response = JSONObject.parseObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
public static CommonResponse getExpress(String no) throws Exception{
String host = "https://wuliu.market.alicloudapi.com";
String path = "/kdi";
String appcode = "70b8fac5a1e84b478b3115f0f3e6e7de";
String urlSend = host + path + "?no=" + no ;
URL url = new URL(urlSend);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Authorization", "APPCODE " + appcode);
int httpCode = httpURLConnection.getResponseCode();
if (httpCode==200){
String json = read(httpURLConnection.getInputStream());
System.out.println("/* 获取服务器响应状态码 200 正常;400 权限错误 ; 403 次数用完; */ ");
System.out.println(httpCode);
System.out.println("/* 获取返回的json */ ");
System.out.print(json);
JSONObject jo = JSONObject.parseObject(json);
int status = jo.getInteger("status");
if (status==0){
String str = jo.getString("result");
JSONObject jo1 = JSONObject.parseObject(str);
String strList = jo1.getString("list");
JSONArray jsonArray = JSONArray.parseArray(strList);
return CommonResponse.ok(jsonArray);
}
return CommonResponse.fail(200,"单号输入有误或暂无物流信息");
}
return CommonResponse.fail();
}
private static String read(InputStream is) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
line = new String(line.getBytes(), "utf-8");
sb.append(line);
}
br.close();
return sb.toString();
}
public static String doGet(String url, List<NameValuePair> values) throws IOException {
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
StringBuilder urlBuffer = new StringBuilder(url);
if (!url.contains("?")) {
urlBuffer.append("?");
}
if (values != null) {
for (NameValuePair nameValuePair : values) {
urlBuffer.append("&").append(nameValuePair.getName()).append("=")
.append(URLEncoder.encode(nameValuePair.getValue(), "UTF-8"));
}
}
HttpGet get = new HttpGet(urlBuffer.toString());
CloseableHttpResponse response = httpClient.execute(get);
return getResponseContent(response);
}
public static String doGet(String path, List<NameValuePair> values,
Map<String, String> headers)
throws Exception {
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
StringBuilder urlBuffer = new StringBuilder(path);
if (!path.contains("?")) {
urlBuffer.append("?");
}
if (values != null) {
for (NameValuePair nameValuePair : values) {
urlBuffer.append("&").append(nameValuePair.getName()).append("=")
.append(URLEncoder.encode(nameValuePair.getValue(), "UTF-8"));
}
}
HttpGet request = new HttpGet(urlBuffer.toString());
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
CloseableHttpResponse response = httpClient.execute(request);
return getResponseContent(response);
}
public static String doPost(String url, List<NameValuePair> values) throws IOException {
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
HttpPost post = new HttpPost(url);
if (values != null) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity((List<? extends NameValuePair>) values,"UTF-8");
post.setEntity(entity);
}
CloseableHttpResponse response = httpClient.execute(post);
return getResponseContent(response);
}
public static String doPostForFlow(String url,String json) throws IOException{
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
HttpPost httpPost = new HttpPost(url);
if(StringUtils.isNotBlank(json)){
StringEntity entity = new StringEntity(json,"UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse response = httpClient.execute(httpPost);
return getResponseContent(response);
}
public static String doPost(String url, List<NameValuePair> values, Map<String, String> headers) throws IOException {
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
HttpPost post = new HttpPost(url);
String charset = "UTF-8";
if (values != null) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values,charset);
post.setEntity(entity);
}
for (Map.Entry<String, String> e : headers.entrySet()) {
post.addHeader(e.getKey(), e.getValue());
}
CloseableHttpResponse response = httpClient.execute(post);
return getResponseContent(response);
}
public static String ignoreGetForHttps(String url, List<NameValuePair> values, boolean isIgnoreSSL) throws
IOException {
enableSSl(isIgnoreSSL);
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
.setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM,
AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
.build();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager)
.setDefaultRequestConfig(config).build();
StringBuilder urlBuffer = new StringBuilder(url);
if (!url.contains("?")) {
urlBuffer.append("?");
}
if (values != null) {
for (NameValuePair nameValuePair : values) {
urlBuffer.append("&").append(nameValuePair.getName()).append("=")
.append(URLEncoder.encode(nameValuePair.getValue(), "UTF-8"));
}
}
HttpGet get = new HttpGet(urlBuffer.toString());
CloseableHttpResponse response = httpClient.execute(get);
return getResponseContent(response);
}
public static String ignoreGetForHttps(String url, boolean isIgnoreSSL) throws
IOException {
enableSSl(isIgnoreSSL);
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
.setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM,
AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
.build();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager)
.setDefaultRequestConfig(config).build();
HttpGet get = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(get);
return getResponseContent(response);
}
public static String ignorePostForHttps(String url, List<NameValuePair> values, boolean isIgnoreSSL)
throws IOException {
enableSSl(isIgnoreSSL);
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
.setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM,
AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
.build();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager)
.setDefaultRequestConfig(config).build();
HttpPost post = new HttpPost(url);
if (values != null) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values,Charset.forName("UTF-8"));
post.setEntity(entity);
}
CloseableHttpResponse response = httpClient.execute(post);
return getResponseContent(response);
}
private static String getResponseContent(CloseableHttpResponse response)
throws IOException {
try {
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity httpEntity = response.getEntity();
httpEntity = new BufferedHttpEntity(httpEntity);
return EntityUtils.toString(httpEntity);
}
} finally {
response.close();
}
return null;
}
public static String post(String URL,JSONObject json) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.setHeader("Content-Type", "application/json");
post.addHeader("Authorization", "Basic YWRtaW46");
String result = "";
try {
StringEntity s = new StringEntity(json.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(s);
HttpResponse httpResponse = client.execute(post);
InputStream inStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
result = strber.toString();
System.out.println(result);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println("请求服务器成功,做相应处理");
} else {
System.out.println("请求服务端失败");
}
} catch (Exception e) {
System.out.println("请求异常");
throw new RuntimeException(e);
}
return result;
}
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://webapi.sms.mob.com/sms/verify");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0");
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("appkey", "301d78dadaf84"));
nvp.add(new BasicNameValuePair("phone", "15829037255"));
nvp.add(new BasicNameValuePair("zone", "86"));
nvp.add(new BasicNameValuePair("code", "992891"));
CloseableHttpResponse response = null;
String postEntity = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvp));
response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
postEntity = EntityUtils.toString(entity,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(postEntity);
net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(postEntity);
String status = jo.getString("status");
System.out.println("status=="+status);
}
public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException{
JSONObject jsonObject = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity !=null) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.parseObject(result);
}
httpGet.releaseConnection();
return jsonObject;
}
public static String doHttpsGetJson(String Url)
{
String message = "";
try
{
System.out.println("doHttpsGetJson");
URL urlGet = new URL(Url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
http.connect();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
message=new String(jsonBytes,"UTF-8");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return message;
}
}