zoukankan      html  css  js  c++  java
  • 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();

    }

  • 相关阅读:
    回望2010,展望2011
    Java加密解密
    纠正平时代码中一些简单的误区(附代码)(不断收集)
    Android模拟器代理上网
    ExifInterface 获取GPS数据
    用异或加密(Java版)
    解决Conversion to Dalvik format failed: Unable to execute dex: null
    Android如何快速卸载apk
    2010年的最后一天,我又辞工(日记)
    项目管理学习资料(经典)
  • 原文地址:https://www.cnblogs.com/songyinan/p/15238825.html
Copyright © 2011-2022 走看看