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方法

转载于:https://www.cnblogs.com/lq147760524/p/8204436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值