一、get方法

 1 package lq.httpclient.method;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.HttpURLConnection;
 7 import java.net.MalformedURLException;
 8 import java.net.URL;
 9 
10 import org.junit.Test;
11 
12 public class HttpGet {
13 
14     @Test
15     public void test() {
16         String url = "http://127.0.0.1:8080/ccb-cloud";
17         try {
18             URL targetUrl = new URL(url);
19             HttpURLConnection connection = (HttpURLConnection)targetUrl.openConnection();
20             connection.setRequestMethod("GET");
21             connection.setRequestProperty("Accept", "application/json");
22             if(connection.getResponseCode() != 200) {
23                 throw new RuntimeException(""+connection.getResponseCode());
24             }
25             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
26             String outPut;
27             while((outPut = bufferedReader.readLine()) != null) {
28                 System.out.println(outPut);
29             }
30         } catch (MalformedURLException e) {
31             e.printStackTrace();
32         } catch (IOException e) {
33             e.printStackTrace();
34         }
35     }
36 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2021-10-11
  • 2022-01-28
  • 2021-11-11
  • 2022-01-11
猜你喜欢
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-12-25
  • 2021-08-09
相关资源
相似解决方案