• HttpClientUtil 工具类 中 用 HttpPost 方式,表单提交参数 来 向 Jersey框架 发送请求 直接 上代码


    //maven 添加========================

    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.69</version>
    </dependency>




    //post 提交方法

    public static String getdoPost(String url, Map<String, String> mapdata) {
    CloseableHttpResponse response = null;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 创建httppost
    HttpPost httpPost = new HttpPost(url);
    try {
    // 设置提交方式
    httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
    // 添加参数
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("data", mapdata.toString()));
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
        //防止中文参数乱码设置
    InputStream contents = httpPost.getEntity().getContent();
    InputStreamReader inputStreamReader = new InputStreamReader(contents, "UTF-8");
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String readLine = bufferedReader.readLine();
    URLDecoder.decode(readLine, "UTF-8");
    // 执行http请求
    response = httpClient.execute(httpPost);
    // 获得http响应体
    HttpEntity entity = response.getEntity();
    if (entity != null) {
    // 响应的结果
    String content = EntityUtils.toString(entity, "UTF-8");
    return content;
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return "获取数据错误";
    }





    //Jersey 接口 接收方法

    @POST
    @Path("/ceshi")
    @Produces("application/json")
    public Response saveWbzlAudit (@FormParam("data")JSONObject data) throws JSONException{

    String userid=data.get("userid").toString();
    String billno=data.get("sex").toString();

    }

  • 相关阅读:
    操作系统基础知识
    os库基本介绍
    原型模式
    ASP .NetCore 部署500错误 查看异常详情
    css设置文本自动换行
    SqlServer数据库链接字符串
    Json列表数据查找更新
    VB中将类标记为可序列化
    VB 性能优化点
    参加公司工作总结会要准备的内容 IT 技术部
  • 原文地址:https://www.cnblogs.com/songyinan/p/15238825.html
Copyright © 2020-2023  润新知