java get post 请求_JAVA Post 和 GET请求

importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.nio.charset.StandardCharsets;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;classResponse {private final intcode;private final byte[] data;

Response(int code, byte[] data) {this.code =code;this.data =data;

}

@OverridepublicString toString() {

StringBuilder sb= new StringBuilder(1024);

sb.append(code).append("\n");

String s= newString(data, StandardCharsets.UTF_8);if (s.length() > 1024) {

sb.append(s.substring(0, 1024)).append("\n...");

}else{

sb.append(s);

}returnsb.toString();

}

}public classHttpClient {public static void main(String[] args) throwsException {

Response resp= get("https://www.douban.com");

System.out.println(resp);

Map postMap = new HashMap<>();

postMap.put("form_email", "test");

postMap.put("form_password", "password");

Response postResp= post("https://www.douban.com/accounts/login","application/x-www-form-urlencoded",

toFormData(postMap));

System.out.println(postResp);

}private staticResponse get(String theUrl) {

System.err.println("GET: " +theUrl);

HttpURLConnection conn= null;try{

URL url= newURL(theUrl);

conn=(HttpURLConnection) url.openConnection();

ByteArrayOutputStream responseBuffer= newByteArrayOutputStream();try (InputStream input =conn.getInputStream()) {byte[] buffer = new byte[1024];for(; ; ) {int n =input.read(buffer);if (n == (-1)) {break;

}

responseBuffer.write(buffer,0, n);

}

}return newResponse(conn.getResponseCode(), responseBuffer.toByteArray());

}catch(IOException e) {throw newRuntimeException(e);

}finally{if (conn != null) {

conn.disconnect();

}

}

}private staticResponse post(String theUrl, String contentType, String contentData) {

System.err.println("POST: " +theUrl);

HttpURLConnection conn= null;try{

URL url= newURL(theUrl);

conn=(HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setDoOutput(true);byte[] postData =contentData.getBytes(StandardCharsets.UTF_8);

conn.setRequestProperty("Content-Type", contentType);

conn.setRequestProperty("Content-Length", String.valueOf(postData.length));try (OutputStream output =conn.getOutputStream()) {

output.write(postData);

}

ByteArrayOutputStream responseBuffer= newByteArrayOutputStream();try (InputStream input =conn.getInputStream()) {byte[] buffer = new byte[1024];for(; ; ) {int n =input.read(buffer);if (n == (-1)) {break;

}

responseBuffer.write(buffer,0, n);

}

}return newResponse(conn.getResponseCode(), responseBuffer.toByteArray());

}catch(IOException e) {throw newRuntimeException(e);

}finally{if (conn != null) {

conn.disconnect();

}

}

}private static String toFormData(Map map) throwsIOException {

List list = new ArrayList<>(map.size());for(String key : map.keySet()) {

list.add(key+ "=" + URLEncoder.encode(map.get(key), "UTF-8"));

}return String.join("&", list);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值