JAVA 代码里HttpURLConnection post方式请求http服务
/** * post方式请求http服务 * * @param urlStr url地址 * @param params 参数,例如:name=yxd&age=25 * @return * @throws Exception */ public static String _getURLByPost(String urlStr, String params) throws Exception { urlStr += "?" + params; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); System.out.println("=====访问的url:" + urlStr); System.out.println("=====访问的params:" + params); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("Content-Type", "application/json"); conn.connect();// 连接 PrintWriter printWriter = new PrintWriter(conn.getOutputStream()); printWriter.write(params); printWriter.flush(); BufferedReader in = null; StringBuilder sb = new StringBuilder(); try { int code = conn.getResponseCode(); System.out.println("=====访问code" + code); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String str = null; while ((str = in.readLine()) != null) { sb.append(str); } } catch (Exception ex) { throw ex; } finally { try { conn.disconnect(); if (in != null) { in.close(); } if (printWriter != null) { printWriter.close(); } } catch (IOException ex) { throw ex; } } return sb.toString(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
2018-08-29 easyui_validatebox常用验证