Maven搭建SSM框架测试HTTP 接口

  • K5_982579
    了解作者
  • 48.2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-25 21:59
    上传日期
本文包含maven搭建SSM单工程、代码自动生成、http接口测试
maven搭建SSM.rar
内容介绍
package com.http.common.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class HttpUtil { /** * * * @Title: getParams * @Description: HTTP GET 请求 * @param: @param uri 请求地址 * @param: @param params 参数串 * @param: @return * @return: String * @user: GR·cheng * */ public static String getParams(String uri, String params) { String result = ""; BufferedReader reader = null; StringBuffer url = new StringBuffer(); try { url.append(uri); url.append("?"); url.append(params);// get的参数 xx=xx&yy=yy URL realUrl = new URL(url.toString()); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("Content-Type","text/html; charset=utf-8"); connection.setRequestProperty("Charset", "UTF-8"); connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); } result = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * * * @Title: postParams * @Description: HTTP POST 请求 * @param: @param uri 请求地址 * @param: @param params 参数串 * @param: @return * @return: String * @user: GR·cheng * */ public static String postParams(String uri, String params) { URL url = null; String result = null; try { url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("POST"); // 发送POST请求必须设置如下两行 connection.setDoOutput(true); connection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter( connection.getOutputStream()); // 发送请求参数 printWriter.write(params);// post的参数 xx=xx&yy=yy // flush输出流的缓冲 printWriter.flush(); // 开始获取数据 BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); result = reader.readLine(); reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * * @Title: postJson * @Description: HTTP POST请求方式提交JSON数据 * @param: @param uri 请求地址 * @param: @param object JSONObject * @param: @return * @param: @throws IOException * @return: String * @user: GR·cheng * */ public static String postJson(String uri, JSONObject object) { String result = null; try { // 创建url资源 URL url = new URL(uri); // 建立http连接 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 设置允许输出 connection.setDoOutput(true); // 设置允许输入 connection.setDoInput(true); // 设置传递方式POST connection.setRequestMethod("POST"); // 设置不用缓存 connection.setUseCaches(false); // 设置本次连接是否自动处理重定向 connection.setInstanceFollowRedirects(true); // 设置文本类型 connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); // 设置维持长连接 connection.setRequestProperty("Connection", "Keep-Alive"); // 设置文本字符集 connection.setRequestProperty("Charset", "UTF-8"); // 转换为字节数组 byte[] json = (JSON.toJSONString(object)).getBytes(); // 设置文本长度 connection.setRequestProperty("Content-Length", String.valueOf(json.length)); // 开始连接请求 connection.connect(); DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write(json); out.flush(); out.close(); int code = connection.getResponseCode(); if (HttpURLConnection.HTTP_OK == code) { BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); result = reader.readLine(); reader.close(); } connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } }
评论
    相关推荐
    • Maven 文档
      NULL 博文链接:https://snowelf.iteye.com/blog/1403222
    • nsis-maven-plugin:从http分叉
      nsis-maven-插件 从派生
    • maven
      NULL 博文链接:https://fypop.iteye.com/blog/2176361
    • Maven
      NULL 博文链接:https://lixue150.iteye.com/blog/1879174
    • maven
      NULL 博文链接:https://jeemygrow.iteye.com/blog/2007145
    • jetty maven pom run
      jetty maven pom run ---------------------------------------- http://knight-black-bob.iteye.com/blog/2360698
    • maven
      NULL 博文链接:https://nancychengnan.iteye.com/blog/2082744
    • maven
      NULL 博文链接:https://jose-bing.iteye.com/blog/1283855
    • cucumber-maven-plugin
      Cucumber Maven 插件 这个 maven 插件允许基于一些Cucumber文件生成文档。 用法: < groupId>org.codehaus.mojo < artifactId>cucumber-maven-... < confluenceUrl>http://confluenceserver
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档