苟有恒何必三更眠五更起     最无益莫过一日曝十日寒

http远程调用原生get、post模板

一、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

二、post方法

 1 package lq.httpclient.method;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStream;
 7 import java.net.HttpURLConnection;
 8 import java.net.MalformedURLException;
 9 import java.net.URL;
10 
11 import org.junit.Test;
12 
13 import com.alibaba.fastjson.JSONObject;
14 
15 public class HttpPost {
16 
17     @Test
18     public void test() {
19         String url = "http://127.0.0.1:8080/ccb-cloud";
20         try {
21             URL targetUrl = new URL(url);
22             HttpURLConnection connection = (HttpURLConnection)targetUrl.openConnection();
23             connection.setRequestMethod("POST");
24             connection.setRequestProperty("Content-Type", "application/json");
25             //允许输入参数
26             connection.setDoOutput(true);
27             JSONObject jsonObject = new JSONObject();
28             jsonObject.put("name", "zhangsan");
29             byte[] bytes = jsonObject.toString().getBytes();
30             //超时20S
31             connection.setConnectTimeout(20000);
32             OutputStream outStream = connection.getOutputStream();
33             outStream.write(bytes);
34             outStream.flush();
35             outStream.close();
36             if(connection.getResponseCode() != 200) {
37                 throw new RuntimeException(""+connection.getResponseCode());
38             }
39             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
40             String outPut;
41             while((outPut = bufferedReader.readLine()) != null) {
42                 System.out.println(outPut);
43             }
44         } catch (MalformedURLException e) {
45             e.printStackTrace();
46         } catch (IOException e) {
47             e.printStackTrace();
48         }finally {
49             System.out.println("ends");
50         }
51     }
52 }
View Code

希望对大家有所帮助,也可以参考封装好的httpClient方法

posted @ 2018-01-05 14:43  月魄  阅读(736)  评论(0编辑  收藏  举报