常用工具记录
发送请求
http://httpbin.org/#/Status_codes
纯Java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class RequestUtil {
private static final String POST = "POST";
private static final String GET = "GET";
public static void appadd(String path, String method , String params) {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(method);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
if (POST.equals(method)) {
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), StandardCharsets.UTF_8);
sb.append(lines);
}
System.out.println(sb);
reader.close();
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final HashMap<String, Object> params = new HashMap<>(3);
appadd("url", POST, mapToJSON(params));
appadd(mapToGetUrl("url", params), GET, null);
}
private static String mapToGetUrl(String url , Map<String, Object> map){
final StringBuilder builder = new StringBuilder(url);
builder.append("?");
map.forEach((key, value) -> {
builder.append(String.format("%s=%s&", key, value));
});
return builder.toString();
}
private static String mapToJSON(Map<String, Object> map){
final StringBuilder builder = new StringBuilder("{");
map.forEach((key, value) -> {
builder.append(String.format("\"%s\":\"%s\",", key, value));
});
builder.append("}");
return builder.toString().replace(",}", "}");
}
}
Hutool
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.8</version>
</dependency>
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class HutoolRequestUtil {
public static void main(String[] args) {
final HashMap<String, Object> params = new HashMap<>();
params.put("key", "value");
doGet("url", params);
doPost("url", params);
}
public static String doGet(String url, Map<String, Object> params){
final String response = HttpUtil.get(url, params);
System.out.println(response);
return response;
}
public static String doPost(String url, Map<String, Object> params){
final HttpRequest request = HttpUtil.createPost(url).body(JSONObject.toJSONString(params));
request.header("Content-Type","application/json");
request.setConnectionTimeout(2000);
final HttpResponse execute = request.execute();
System.out.println(execute.body());
return execute.body();
}
}
|