public String httpGetTest(String token,String urls,String method){ try { StringBuffer sbf = new StringBuffer(); String strRead = null; URL url = new URL(urls); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET");//请求post方式 connection.setDoInput(true); connection.setDoOutput(true); //header内的的参数在这里set。||connection.setRequestProperty("健, "值"); 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.setRequestProperty("Content-Type", "application/json;charset=utf-8"); connection.setRequestProperty("Authorization","Bearer "+ token); connection.connect(); InputStream is = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((strRead = reader.readLine()) != null) { sbf.append(strRead); } reader.close(); connection.disconnect(); String results = sbf.toString(); return results; }catch ( MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
尝试java调取接口并修改header
最新推荐文章于 2024-07-26 17:03:46 发布