1. 程式人生 > >服務間http呼叫簡單案例(通過httpUrlConnection)

服務間http呼叫簡單案例(通過httpUrlConnection)

URLConnections 類方法 openConnection() 返回一個 java.net.URLConnection。 例如: 如果你連線HTTP協議的URL, openConnection() 方法返回 HttpURLConnection 物件。 如果你連線的URL為一個 JAR 檔案, openConnection() 方法將返回 JarURLConnection 物件。 程式碼如下:

客戶端
package com.markor.url;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: caichangmeng <
[email protected]
> * @date: 2018/10/22 * @module: 類所屬模組 * @describe: urlConnection 練習 * @version: v1.0 */ public class UrlConnectionTest { public static void sendDemo(String targetUrl, String requestMethod, String contentType, String outputStr) throws IOException { URL url = new URL(targetUrl); URLConnection urlConnection = url.openConnection(); HttpURLConnection connection = null; if (urlConnection instanceof HttpURLConnection) { connection = (HttpURLConnection) urlConnection; connection.setRequestMethod(requestMethod); } else { System.out.println("url 非 http請求"); return; } if (StringUtils.equalsIgnoreCase("json", contentType)) { connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); } connection.setDoInput(true); connection.setDoOutput(true); connection.connect(); //往伺服器端寫內容 也就是發起http請求需要帶的引數 if (null != outputStr) { OutputStream os = connection.getOutputStream(); os.write(outputStr.getBytes("utf-8")); //os.close(); } System.out.println("contentType: " + connection.getContentType()); BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuffer sb = new StringBuffer(); String current; while ((current = br.readLine()) != null) { // sb.append(current); System.out.println(current); } System.out.println(sb.toString()); } @Test public void notArgus() throws IOException { sendDemo("http://localhost:8081/notArgus", "GET", null, null); } /** * @Date: 2018/10/22 * @describe: get - 傳值. */ @Test public void argus() throws IOException { sendDemo("http://localhost:8081/argus?path=path-success", "GET", null, null); } /** * @Date: 2018/10/22 * @describe: post. */ @Test public void postNotArgus() throws IOException { sendDemo("http://localhost:8081/postNotArgus", "POST", null, null); } /** * @Date: 2018/10/22 * @describe: post - 表單模式傳值. */ @Test public void postArgus() throws IOException { sendDemo("http://localhost:8081/postArgus", "POST", null, "pageNum=1&pageSize=10"); } /** * @Date: 2018/10/22 * @describe: post, json模式傳值. */ @Test public void postJson() throws IOException { Map<String, Integer> map = new HashMap<>(); map.put("pageSize", 10); map.put("pageNum", 1); sendDemo("http://localhost:8081/postJson", "POST", "json", JSON.toJSONString(map)); } }
服務端
package com.markor.template.controller.url;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: caichangmeng <
[email protected]
> * @date: 2018/10/22 * @module: 類所屬模組 * @describe: * @version: v1.0 */ @RestController public class UrlController { /** * @Date: 2018/10/22 * @describe: 無參請求 - get * @return : success: 成功 * @throws: * @since: 1.0.0 * @JIRA: */ @GetMapping("notArgus") public String notArgus() { return "get - success"; } /** * @Date: 2018/10/22 * @describe: 有參請求 * @param path : * @return : path * @throws: * @since: 1.0.0 * @JIRA: */ @GetMapping("argus") public String argus(String path) { return path; } /** * @Date: 2018/10/22 * @describe: 無參請求 - post * @param null : * @return : null * @throws: * @since: 1.0.0 * @JIRA: */ @PostMapping("postNotArgus") public String postNotArgus() { return "post - success"; } @PostMapping("postArgus") public UrlBean postArgus(UrlBean bean) { return bean; } @PostMapping("postJson") public String postJson(@RequestBody Map<String, Integer> param) { int pageNum = param.get("pageNum"); int pageSize = param.get("pageSize"); return "pageNum: "+ pageNum +"; pageSize: " + pageSize; } }