Java Post请求工具类,非常实用,建议收藏

核心代码:

import java.util.Map; import java.util.List; import java.util.Iterator; import java.util.ArrayList; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.io.ByteArrayOutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; public class PostUtil { public static final String UTF8 = “utf-8”; private static final String CONTENT_TYPE = “application/json”; public static String send(String url, Map<string, string=””> headers, Object params) { try { if (params instanceof Map) return sendMap(url, headers, (Map<string, object=””>)params); else return sendJson(url, headers, (String)params); } catch (Exception e){ return null; } } public static String sendMap(String url, Map<string, string=””> headers, Map<string, object=””> params) throws IOException { List pairs = new ArrayList<>(); for (Iterator iterator = params.keySet().iterator(); iterator.hasNext();) { String name = iterator.next(); Object value = params.get(name); pairs.add(new BasicNameValuePair(name, String.valueOf(value))); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, UTF8); return doAction(url, headers, entity); } public static String sendJson(String url, Map<string, string=””> headers, String params) throws IOException { StringEntity entity = new StringEntity(params, UTF8); return doAction(url, headers, entity); } private static String doAction(String url, Map<string, string=””> headers, StringEntity body) throws IOException { HttpPost post = new HttpPost(url); post.setEntity(body); for (String key : headers.keySet()) post.addHeader(key, headers.get(key)); CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String result = null; if (entity != null) result = EntityUtils.toString(entity, UTF8); response.close(); return result; } public static String postJson(String url, String data) { HttpClientBuilder builder = HttpClientBuilder.create(); HttpUriRequest request = null; HttpEntity entity = new StringEntity(data, UTF8); HttpPost post = new HttpPost(url); post.setEntity(entity); request = post; request.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE); try (CloseableHttpClient closeableHttpClient = builder.build()) { HttpResponse resp = closeableHttpClient.execute(request); InputStream respIs = resp.getEntity().getContent(); byte[] respBytes = toByteArray(respIs); String result = new String(respBytes, Charset.forName(UTF8)); return result; } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] toByteArray(InputStream is) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { byte[] b = new byte[4096]; boolean var3 = false; int n; while ((n = is.read(b)) != -1) { output.write(b, 0, n); } byte[] var4 = output.toByteArray(); return var4; } finally { output.close(); } } }</string,></string,></string,></string,></string,></string,>

路漫漫,我将上下求索

翻译:追求真理,前路漫漫,但是我会不屈不挠、不遗余力地去追寻、去探索。

如果你还有更好的思路和办法可以在评论区里留言大家一起来探讨一下吧~

原文链接:http://www.sfdkj.com/19920.html

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片