概述
package com.hcmony.; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.Map; /** * @FileName package com.hcmony.guide.web.getui.HttpUtil.java * @Creator hcmony * @Created 2017-09-26 10:26:00 * @Modifier hcmony * @Updated 2017-09-26 10:26:00 * @Description * @Version BUILD1001 */ public class HttpUtil { static ObjectMapper mapper = new ObjectMapper(); public static Map<String, Object> httpPostJSON(String httpUrl, Map<String, Object> jsondata,String token) throws JsonGenerationException, JsonMappingException, IOException { BufferedReader reader = null; String result = null; Map retMap = null; StringBuffer sbf = new StringBuffer(); String httpArg = mapper.writeValueAsString(jsondata); byte[] data = httpArg.getBytes("UTF-8"); try { URL url = new URL(httpUrl); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8"); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); // 填入appkey到HTTP header 改 connection.setRequestProperty("access_token", token); connection.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; }}); connection.setDoOutput(true); connection.getOutputStream().write(httpArg.getBytes("UTF-8")); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("rn"); } reader.close(); result = sbf.toString(); if(result != null && !"".equals(result)) { retMap = (Map)mapper.readValue(result, Map.class); return retMap; } else { return null; } } catch (Exception e) { e.printStackTrace(); } return retMap; } public static Map<String, Object> httpPostJSON1(String httpUrl, Map<String, Object> jsondata) throws JsonGenerationException, JsonMappingException, IOException { BufferedReader reader = null; String result = null; Map retMap = null; StringBuffer sbf = new StringBuffer(); String httpArg = mapper.writeValueAsString(jsondata); byte[] data = httpArg.getBytes("UTF-8"); try { URL url = new URL(httpUrl); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8"); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); connection.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; }}); connection.setDoOutput(true); connection.getOutputStream().write(httpArg.getBytes("UTF-8")); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("rn"); } reader.close(); result = sbf.toString(); if(result != null && !"".equals(result)) { retMap = (Map)mapper.readValue(result, Map.class); return retMap; } else { return null; } } catch (Exception e) { e.printStackTrace(); } return retMap; } }
最后
以上就是苹果唇膏为你收集整理的java实现https请求的全部内容,希望文章能够帮你解决java实现https请求所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复