1.添加依赖:
<!--okhttp3--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.1</version> </dependency> <!-- http --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> </dependency>
2.HttpUtils类
import okhttp3.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class HttpUtils {
public static String httpGetMethod(String url, Map<String, String> params)
throws IOException {
OkHttpClient client = new OkHttpClient();
StringBuffer paramStr = new StringBuffer();
paramStr.append("?");
params.keySet().stream().forEach(key -> {
paramStr.append(key + "=" + params.get(key) + "&");
});
paramStr.setLength(paramStr.length() - 1);
Request request = new Request.Builder().url(url + paramStr.toString())
.get().build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String httpPostParams(String url, Map<String, String> params)
throws IOException {
OkHttpClient client = new OkHttpClient();
StringBuffer paramStr = new StringBuffer();
paramStr.append("?");
params.keySet().stream().forEach(key -> {
paramStr.append(key + "=" + params.get(key) + "&");
});
paramStr.setLength(paramStr.length() - 1);
System.out.println("测试:" + url + paramStr.toString());
Request request = new Request.Builder().url(url + paramStr.toString())
.post(okhttp3.internal.Util.EMPTY_REQUEST).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String httpPostStringParamsAndlongParams(String url, Map<String, String> Stringparams,Map<String, Long> Longparams)
throws IOException {
OkHttpClient client = new OkHttpClient();
StringBuffer paramStr = new StringBuffer();
paramStr.append("?");
Stringparams.keySet().stream().forEach(key -> {
paramStr.append(key + "=" + Stringparams.get(key) + "&");
});
Longparams.keySet().stream().forEach(key -> {
paramStr.append(key + "=" + Stringparams.get(key) + "&");
});
paramStr.setLength(paramStr.length() - 1);
System.out.println("测试:" + url + paramStr.toString());
Request request = new Request.Builder().url(url + paramStr.toString())
.post(okhttp3.internal.Util.EMPTY_REQUEST).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String httpPostMethod(String url, Map<String, String> params)
throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).build();
FormBody.Builder formBody = new FormBody.Builder();
params.keySet().stream().forEach(key -> {
formBody.add(key, params.get(key));
});
Request request = new Request.Builder().url(url).post(formBody.build())
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String httpPostMethodWithUtf8(String url,
Map<String, String> params) throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).build();
FormBody.Builder formBody = new FormBody.Builder();
params.keySet().stream().forEach(key -> {
try {
formBody.add(key, URLEncoder.encode(params.get(key), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
});
Request request = new Request.Builder().url(url).post(formBody.build())
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String httpPostMethod(String url,
Map<String, String> headerMap, Map<String, String> params)
throws IOException {
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBody = new FormBody.Builder();
Headers headers = Headers.of(headerMap);
params.keySet().stream().forEach(key -> {
formBody.add(key, params.get(key));
});
Request request = new Request.Builder().url(url).headers(headers)
.post(formBody.build()).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static Response httpPostResponse(String url, Map<String, String> headers,
String requestBodys) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType
.parse(headers.get("Content-Type") == null ? "application/json"
: headers.get("Content-Type"));
RequestBody requestBody = RequestBody.create(mediaType, requestBodys);
Headers headerMap = okhttp3.Headers.of(headers);
Request request = new Request.Builder().url(url).headers(headerMap)
.post(requestBody).build();
Response response = client.newCall(request).execute();
return response;
}
public static String httpPostMethod(String url, Map<String, String> headers,
String requestBodys) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType
.parse(headers.get("Content-Type") == null ? "application/json"
: headers.get("Content-Type"));
RequestBody requestBody = RequestBody.create(mediaType, requestBodys);
Headers headerMap = okhttp3.Headers.of(headers);
Request request = new Request.Builder().url(url).headers(headerMap)
.post(requestBody).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String postMethod(String url, Map<String, String> headers,
InputStream bodyContent) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType
.parse(headers.get("Content-Type") == null ? "application/json"
: headers.get("Content-Type"));
int total = (bodyContent.available() / 1024 + 1) * 1024;
byte b[] = new byte[total];
byte r[] = new byte[1024];
int len = 0;
int temp = 0; // 所有读取的内容都使用temp接收
while ((temp = bodyContent.read(r)) != -1) { // 当没有读取完时,继续读取
System.arraycopy(r, 0, b, 1024 * len, 1024);
len++;
}
RequestBody requestBody = RequestBody.create(mediaType, b);
Headers headerMap = okhttp3.Headers.of(headers);
Request request = new Request.Builder().url(url).headers(headerMap)
.post(requestBody).build();
Response response = client.newCall(request).execute();
bodyContent.reset();
return response.body().string();
}
public static String clientPostMethod(String url,
Map<String, String> headers, InputStream bodyContent)
throws IOException {
HttpPost method = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
headers.keySet().stream().forEach(head -> {
method.addHeader(new BasicHeader(head, headers.get(head)));
});
if (bodyContent != null) {
method.setEntity(new InputStreamEntity(bodyContent, -1));
}
HttpResponse postResponse = httpClient.execute(method);
return getResponsBodyAsString(postResponse.getEntity().getContent());
}
public static String clientPostMethod(String url,
Map<String, String> headers) throws IOException {
HttpPost method = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
headers.keySet().stream().forEach(head -> {
method.addHeader(new BasicHeader(head, headers.get(head)));
});
HttpResponse postResponse = httpClient.execute(method);
return getResponsBodyAsString(postResponse.getEntity().getContent());
}
private static String getResponsBodyAsString(InputStream input)
throws IOException {
String responsBodyString = null;
try {
Reader reader = new InputStreamReader(input, "utf-8");
StringBuilder b = new StringBuilder();
char[] c = new char[1024];
int len;
while (0 < (len = reader.read(c))) {
b.append(c, 0, len);
}
responsBodyString = b.toString();
} finally {
input.close();
}
return responsBodyString;
}
}
3.使用:
import com.alibaba.fastjson.JSONObject;
import com.xx.xx.constant.WxConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class WeiXinUtil {
public static Log log = LogFactory.getLog(WeiXinUtil.class);
public static String token=null;
public static String getAccessToken(){
Map<String, String> params = new HashMap<>();
params.put("corpid", WxConstants.CORPID);//常量类中的CORPID常量
params.put("corpsecret", WxConstants.SECRET);//常量类中的SECRET常量
//GET_ACCESS_TOKEN_URL是企业微信获取access_token url
try {
String responseBody = HttpUtils.httpGetMethod(WxConstants.GET_ACCESS_TOKEN_URL, params);
if (responseBody != null) {
JSONObject tokenBody = JSONObject.parseObject(responseBody);
token = tokenBody.getString("access_token");
System.out.println("获取token=="+token);
}
} catch (IOException e) {
e.printStackTrace();
}
return token;
}
public static void main(String[] args){
log.info("运行===");
getAccessToken();
}
}
4,运行
WeiXinUtil类:

就可以从企业微信中获取access_token了。
本文介绍了一个实用的HTTP工具类,包括GET和POST请求的多种实现方式,如使用OkHttp和Apache HttpClient。此外,还展示了如何通过调用企业微信API获取access_token,适用于需要与企业微信进行交互的场景。
3894

被折叠的 条评论
为什么被折叠?



