JSON

HttpClient 以及 Json 传递的一些坑

字号+ 作者:H5之家 来源:H5之家 2017-10-16 18:13 我要评论( )

本文介绍一下使用中关于HttpClient以及Json传递的坑。

原文出处: Hosee

背景:

记录一下使用中关于HttpClient以及Json传递的坑。

HTTPS:

普通方式:

public class Test { public static void main(String[] args) throws Exception { URI uri = new URIBuilder().setScheme("http").setHost("***:**") .setPath("/***/***").build(); HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("params", "test")); HttpResponse httpResponse = httpClient.execute(httpPost); StatusLine httpStatus = httpResponse.getStatusLine(); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println("httpStatusline: " + httpStatus); System.out.println("strEntity: " + EntityUtils.toString(httpEntity)); EntityUtils.consume(httpEntity); } }

这种适合普通的http的请求,当把Scheme换成https时,

报错:

sun.security.validator.ValidatorException: PKIX path building failed

客户端没有证书,可以在代码中跳过证书验证。

public static String sendPost(final URI uri, final List<NameValuePair> params) throws ClientProtocolException, IOException, NoSuchAlgorithmException, KeyManagementException { String result = null; SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }, new SecureRandom()); CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(new SSLSocketFactory(sslContext)).build(); HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("Content-type", "application/json"); httpPost.setEntity(new UrlEncodedFormEntity(params)); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); EntityUtils.consume(entity); } finally { response.close(); } return result; } request.getParameter无法得到appliation/json的数据 @ResponseBody @RequestMapping(value = "/**/**.json", method = RequestMethod.POST) public String valiate(ModelMap map, HttpServletRequest request) { System.out.println(request.getParameter("param")); }

这种方式无法得到Content-Type是appliation/json的数据。

这种方式适合Content-Type为application/x-www-form-urlencoded的请求。

解决:

@ResponseBody @RequestMapping(value = "/**/**.json", method = RequestMethod.POST ) public String valiate(ModelMap map, @RequestBody String request) { JSONObject requestJson = JSON.parseObject(request); }

这里需要说明的是@RequestBody需要接的参数是一个string化的json,而不是一个json对象,也可以用对象类型来接收。

Reference:

1.

2.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • json数据反序列化的问题

    json数据反序列化的问题

    2017-10-16 18:14

  • Ajax返回html和json格式数据

    Ajax返回html和json格式数据

    2017-10-16 17:00

  • Android笔记(五十) Android中的JSON数据

    Android笔记(五十) Android中的JSON数据

    2017-10-16 17:00

  • python 读取json

    python 读取json

    2017-10-16 14:28

网友点评