设为首页 加入收藏

TOP

http远程调用原生get、post模板
2019-09-17 19:05:42 】 浏览:26
Tags: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方法

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java 快速开发平台 有代码生成器 .. 下一篇《PHP扩展及核心》

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目