备案/许可证编号:粤ICP备17106855号,工信部网站(http://www.beian.miit.gov.cn)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.util.Map.Entry;import com.alibaba.fastjson.JSONObject;/** * @description: * @author: chenchifeng * @create: 2019-11-18 11:08 **/public class HttpUtil { public static void main(String[] args) { String url = "http://localhost:8080/"; JSONObject params = new JSONObject(); params.put("type", 1); params.put("value", true); System.out.println(doGet(url, params)); } /** POST请求 */ public static JSONObject doPost(String url, JSONObject params) { JSONObject result = new JSONObject(); result.put("success", true); result.put("data", null); result.put("code", 200); result.put("msg", null); OutputStream out = null; DataOutputStream dataOutputStream = null; InputStream in = null; ByteArrayOutputStream baos = null; try { URLConnection urlConnection = new URL(url).openConnection(); HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; // 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内,因此需要设为true, // 默认情况下是false; httpUrlConnection.setDoOutput(true); // 设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true); // 忽略缓存 httpUrlConnection.setUseCaches(false); // 设定请求的方法为"POST",默认是GET httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.connect(); // 建立输入流,向指向的URL传入参数 String queryString = ""; if (params != null) { for (Entry<String, Object> entry : params.entrySet()) { queryString += entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString(), "UTF-8") + "&"; } } if (queryString.length() > 0) { queryString = queryString .substring(0, queryString.length() - 1); out = httpUrlConnection.getOutputStream(); dataOutputStream = new DataOutputStream(out); dataOutputStream.writeBytes(queryString); dataOutputStream.flush(); out.flush(); } // 获得响应状态 int responseCode = httpUrlConnection.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) { baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; in = httpUrlConnection.getInputStream(); while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); baos.flush(); } result.put("success", true); result.put("data", baos.toString("UTF-8")); result.put("code", 200); result.put("msg", "请求成功"); } else { result.put("success", false); result.put("code", responseCode); result.put("msg", "请求异常"); } } catch (Exception e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } if (in != null) { try { in.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } if (dataOutputStream != null) { try { dataOutputStream.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } if (out != null) { try { out.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } } return result; } /** GET请求 */ public static JSONObject doGet(String url, JSONObject params) { JSONObject result = new JSONObject(); result.put("success", true); result.put("data", null); result.put("code", 200); result.put("msg", null); InputStream in = null; ByteArrayOutputStream baos = null; try { // URL传入参数 String queryString = ""; if (params != null) { for (Entry<String, Object> entry : params.entrySet()) { queryString += entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString(), "UTF-8") + "&"; } } if (queryString.length() > 0) { queryString = queryString .substring(0, queryString.length() - 1); url = url + "?" + queryString; } URLConnection urlConnection = new URL(url).openConnection(); HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; // 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内,因此需要设为true, // 默认情况下是false; httpUrlConnection.setDoOutput(false); // 设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true); // 忽略缓存 httpUrlConnection.setUseCaches(false); // 设定请求的方法为"POST",默认是GET httpUrlConnection.setRequestMethod("GET"); httpUrlConnection.connect(); // 获得响应状态 int responseCode = httpUrlConnection.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) { baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; in = httpUrlConnection.getInputStream(); while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); baos.flush(); } result.put("success", true); result.put("data", baos.toString("UTF-8")); result.put("code", 200); result.put("msg", "请求成功"); } else { result.put("success", false); result.put("code", responseCode); result.put("msg", "请求异常"); } } catch (Exception e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } if (in != null) { try { in.close(); } catch (IOException e) { result.put("success", false); result.put("code", 500); result.put("msg", "请求异常,异常信息:" + e.getClass() + "->" + e.getMessage()); } } } return result; }}
互联网上搜索而至,如有略同请通知作者。