java访问外部接口 简单实现

 1 /*使用时注意字符集 "GBK""UTF-8"*/
 2 public static String visitPost(String urlStr, String code) {
 3         try{
 4             URL url = new URL(urlStr);
 5             HttpURLConnection con = (HttpURLConnection)url.openConnection();
 6             con.setRequestMethod("GET");
 7             con.connect();
 8             BufferedReader reader = new BufferedReader(
 9                                         new InputStreamReader(
10                                                 con.getInputStream(),code));));
11             String line;
12             StringBuffer buffer = new StringBuffer();
13             while((line = reader.readLine()) != null) {
14                 buffer.append(line);
15             }
16             reader.close();
17             con.disconnect();
18             String res = buffer.toString();
19             return res;
20         } catch(Exception e) {
21             e.printStackTrace();
22         }
23         return null;
24     }

 

转载于:https://www.cnblogs.com/dougest/p/6580960.html

你可能感兴趣的:(java访问外部接口 简单实现)