java调用第三方接口(项目亲测)

时间:2025-01-30 12:34:12
import com.alibaba.fastjson.JSONObject; import com.aliyun.openservices.eas.predict.http.HttpConfig; import com.aliyun.openservices.eas.predict.http.PredictClient; import lombok.extern.slf4j.Slf4j; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; /** * @author buH * #data 2020/12/4 10:07 * 接口工具类 */ @Slf4j public class HttpUtil { /** * post请求 * * @param URL 请求url * @param param 参数 * @param token 请求携带的token(如果需要) */ public static String doPost(String URL, JSONObject param, String token) { log.info("request address:"+URL); OutputStreamWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); HttpURLConnection conn = null; try { java.net.URL url = new URL(URL); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); if (token != null) { conn.setRequestProperty("token", token); } //获取输出流 if (param != null) { out = new OutputStreamWriter(conn.getOutputStream()); String jsonStr = param.toString(); out.write(jsonStr); out.flush(); out.close(); } //取得输入流,并使用Reader读取 if (200 == conn.getResponseCode()) { in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = in.readLine()) != null) { result.append(line); } } else { System.out.println("ResponseCode is an error code:" + conn.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } return result.toString(); } /** * 向指定URL发送GET方法的请求 * * @param baseUrl 发送请求的URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return result 所代表远程资源的响应结果 */ public static String doGet(String baseUrl, String param) { String result = ""; BufferedReader in = null; try { String urlNameString =baseUrl; if(param!=null){ urlNameString = baseUrl + "?" + param; } URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); connection.setRequestProperty("Charsert", "ISO-8859-1"); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } }else{ return "Code="+connection.getResponseCode(); } } catch (Exception e) { e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }