日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java实现向指定URL用POST方法发送Json格式字符串参数请求的工具类

發布時間:2025/3/19 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现向指定URL用POST方法发送Json格式字符串参数请求的工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

SpringBoot項目中通過后臺Controller向某服務接口發送POST請求。

實現

在項目pom.xml中添加依賴

?<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>2.2</version><scope>compile</scope></dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.6.0</version></dependency>

在util包下新建HttpRequestUtil.java

public class HttpRequestUtil {/*** 定義全局OkHttpClient對象*/private static final OkHttpClient httpClient = new OkHttpClient();/*** 向指定 URL 發送POST方法的請求** @param url*??????????? 發送請求的 URL* @param param*??????????? 請求參數,請求參數應該是Json格式字符串的形式。* @return 所代表遠程資源的響應結果*/public static String sendPost(String url, String jsonData) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打開和URL之間的連接URLConnection con = realUrl.openConnection();HttpURLConnection conn = (HttpURLConnection) con;// 設置通用的請求屬性conn.setRequestMethod("POST"); // 設置Post請求conn.setConnectTimeout(5 * 1000);conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); //??????????? conn.setRequestProperty("Content-Type", //??????????????????? "application/x-www-form-urlencoded"); // 設置內容類型conn.setRequestProperty("Content-Type","application/json"); // 設置內容類型// conn.setRequestProperty("Content-Length",// String.valueOf(param.length())); //設置長度// 發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);// 獲取URLConnection對象對應的輸出流out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));// 發送請求參數// out.print(param);out.write(jsonData);// flush輸出流的緩沖out.flush();// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}byte[] bresult = result.getBytes();result = new String(bresult, "utf-8");} catch (Exception e) {System.out.println("發送 POST 請求出現異常!" + e);e.printStackTrace();}// 使用finally塊來關閉輸出流、輸入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}}

?

注意根據請求的enctype進行相應的設置

conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); // 設置內容類型

或者
?

conn.setRequestProperty("Content-Type","application/json"); // 設置內容類型

在后臺Controller調用時只需要:

?

String URL="http://IP:7788/PostPrintService/Info";Map map=new HashMap();map.put("TableName","wms_receive_order_details");map.put("PrintID",PrintId);String? param= JSON.toJSONString(map);String message = HttpRequestUtil.sendPost(URL, param);

?

總結

以上是生活随笔為你收集整理的Java实现向指定URL用POST方法发送Json格式字符串参数请求的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。