一、使用commons-httpclient发送http请求
- 引入pom文件
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
- 创建HttpClient对象实例化对象,分别对post和get请求进行处理
public class TestHttpClient {
public static void main(String[] args) throws IOException {
String sign = HeaUtil.sha256_HMAC("access_token=U1RzaWQwMDAwMDAxNjQ4MDI0OTk5M23jI4TUtBQmZhdG9uU3Zid3Z3dzlndXBjNUhxenBWdTg1S3F8fDF8djJ8MQ==&openid=120D1802840AAC2CFEEC20DA9BE4E34188Ac00330919c90492f1b28c05c69d9c4c1030", "you-string");
String url = "https://68b22a800-20883-46251-a9f2-463dde5412244a.bspapp.com/loginPath?access_token=U1RzaWQwMDAwMDAxNjQ42MDI0OTk5MjI4TUtBQmZ2hdG9uU3Zid3Z3dzl2ndXBjNUhxenBWdTg1S3F8fDF8djJ8MQ==&openid=1202D1802840AAC2CFEEC20D2A9BE4E34188Ac003309129c90492fb28c05c69d9c4c1030&sign=" + sign;
System.out.println(sendPost(url ));
System.out.println(sendGet(url ));
}
public static String sendPost(String urlParame) throws IOException {
HttpClient httpClient=new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
PostMethod postMethod=new PostMethod(urlParame);
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,10000);
postMethod.addRequestHeader("Content-Type", "application/json");
httpClient.executeMethod(postMethod);
String result=postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
}
public static String sendGet(String urlParame) throws IOException {
HttpClient httpClient=new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
GetMethod getMethod=new GetMethod(urlParame);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,10000);
getMethod.addRequestHeader("Content-Type", "application/json");
httpClient.executeMethod(getMethod);
String result=getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return result;
}
}
public class HeaUtil {
public static String sha256_HMAC(String message, String secret) {
String hash = "";
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
hash = byteArrayToHexString(bytes);
} catch (Exception e) {
System.out.println("Error HmacSHA256 ===========" + e.getMessage());
}
return hash;
}
private static String byteArrayToHexString(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1)
hs.append('0');
hs.append(stmp);
}
return hs.toString().toLowerCase();
}
}
二、使用okhttp3发送http请求
- 引入pom文件
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.5.0</version>
</dependency>
- 对get请求进行处理
public static void main(String[] args) {
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request build = builder.get().url("你的url").build();
Call call = okHttpClient.newCall(build);
call.execute();
}