其他分享
首页 > 其他分享> > HttpClientUtil 工具类 中 用 HttpPost 方式,表单提交参数 来 向 Jersey框架 发送请求 直接 上代码

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();

}

标签:UTF,String,HttpClientUtil,data,toString,httpPost,new,HttpPost,Jersey
来源: https://www.cnblogs.com/songyinan/p/15238825.html