JAVA使用HTTP调用接口
httpUtil:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; public class HttpClient { public String post(String requestUrl, String requestPram,String accessToken){ OutputStreamWriter outputStreamWriter = null; BufferedReader bufferedReader = null; StringBuffer responseResult = new StringBuffer(); HttpURLConnection httpURLConnection = null; try { URL realUrl = new URL(requestUrl); httpURLConnection = (HttpURLConnection) realUrl.openConnection(); httpURLConnection.setRequestProperty("accept", "*/*"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("connection", "Keep-Alive"); if(!accessToken.equals("")||accessToken!=null){ httpURLConnection.setRequestProperty("access-token", accessToken); } httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length())); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); outputStreamWriter.write(requestPram); outputStreamWriter.flush(); bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); String line; while ((line = bufferedReader.readLine()) != null) { responseResult.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { httpURLConnection.disconnect(); try { if (outputStreamWriter != null) { outputStreamWriter.close(); } if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return responseResult.toString(); } public String postJson(String requestUrl, String requestPram,String accessToken){ OutputStreamWriter outputStreamWriter = null; BufferedReader bufferedReader = null; StringBuffer responseResult = new StringBuffer(); HttpURLConnection httpURLConnection = null; try { URL realUrl = new URL(requestUrl); httpURLConnection = (HttpURLConnection) realUrl.openConnection(); httpURLConnection.setRequestProperty("accept", "*/*"); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("connection", "Keep-Alive"); if(!accessToken.equals("")||accessToken!=null){ httpURLConnection.setRequestProperty("access-token", accessToken); } httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length())); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); outputStreamWriter.write(requestPram); outputStreamWriter.flush(); bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); String line; while ((line = bufferedReader.readLine()) != null) { responseResult.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { httpURLConnection.disconnect(); try { if (outputStreamWriter != null) { outputStreamWriter.close(); } if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return responseResult.toString(); } public String get(String url, String param) throws Exception { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 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(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { //System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } }
使用:
postJson:
HttpClient httpClient = new HttpClient(); String param = "{\"appKey\":\""+appKey+"\"," + "\"signature\":\""+signature+"\"," + "\"time\":\""+time+"\"}"; String resJson =httpClient.postJson(url+"/api/token",param,""); JSONObject bodyJson = (JSONObject) JSONObject.parse(resJson);
get:
String cityPram = "appKey=yongjia_pro&token="+XinLingTask.getToken(); HttpClient httpTest = new HttpClient(); String cityRes = httpTest.get(XinLingTask.url+"/addition/577/5775/api/dailyCity",cityPram);
//调用返回HTML
public static String getHTMLResourceByUrl(String url,String encoding){ StringBuffer sb = new StringBuffer(); URL urlObj =null; URLConnection openConnection =null; InputStreamReader isr = null; BufferedReader br = null; try { urlObj = new URL(url); openConnection = urlObj.openConnection(); isr = new InputStreamReader(openConnection.getInputStream(),encoding); br = new BufferedReader(isr); String temp = null; while((temp=br.readLine())!=null){ sb.append(temp+"\n"); } } catch (MalformedURLException e) { log.error("error message", e); } catch (IOException e) { log.error("error message", e); }finally{ try { if(isr !=null){ isr.close(); } } catch (IOException e) { log.error("error message", e); } } return sb.toString(); }
HTTPClient调用https请求,通过基本认证用户名密码(Basic Auth)
参考:https://www.cnblogs.com/rinack/p/7568020.html
https://blog.csdn.net/qq_27605885/article/details/79131483
生命毋需绚烂,只要活出自我

浙公网安备 33010602011771号