yanghui0 2020-04-27
1、maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.10</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version></dependency>2、代码
package com.antaiib.atenergy.dao;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class HttpURLConnectionHelper { public static String sendRequest(String urlParam, String requestType) { HttpURLConnection con = null; BufferedReader buffer = null; StringBuffer resultBuffer = null; try { URL url = new URL(urlParam); //得到连接对象 con = (HttpURLConnection) url.openConnection(); //设置请求类型 con.setRequestMethod(requestType); //设置请求需要返回的数据类型和字符集类型 con.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //允许写出 con.setDoOutput(true); //允许读入 con.setDoInput(true); //不使用缓存 con.setUseCaches(false); //得到响应码 int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { //得到响应流 InputStream inputStream = con.getInputStream(); //将响应流转换成字符串 resultBuffer = new StringBuffer(); String line; buffer = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); while ((line = buffer.readLine()) != null) { resultBuffer.append(line); } return resultBuffer.toString(); } } catch (Exception e) { e.printStackTrace(); } return ""; } public static void main(String[] args) throws UnsupportedEncodingException { String unitCode = "520200000000"; String content = "hUEOFUcXHTfag+bxy4NNlYfglwiCMdBk6Fp9mVFKAByg4Q0BSNHAq5wfVM2Ue1+WfZzCHAvUjB1wkauf9Vgu4MKTxOYaG1zOlTwz1erV1BE6UoumllHvQ1HBxbQ1PmcX70a/PywLZsTKgLXnWvduXppvWStaD/s12inlDrhtjJQ8uOA678xFsqfX3AQ0zdPirWLon/TLoC9oNct+kBdUy8T5Vuwy6x2PkrVqvq52zU12HPb+ADV5jHHrddpG8THMd0fqn/L7wIONcpajIAaUxH0TZOqkfiuUM+QZMdzSY58dbJZaB0daSjsxG8LCK7zPbgRSfaHwOsUMKXBjZ1S+9Q3ICxb+ONdoV705aSKvLid64POBS9oifMoq7gCsNubGZZBpbqahOSny2dd7cOsqTeFoXO1w/lpFxU7Qy+g443SpYKR3tWeydlYE3jt1Yci4xD8IJxejxgN7C1OR4kE0bTMSxZ1dpMweOUzhddhJFRQKptSOp2vo8Qk/jESj301IqNnUq8liPlEzCsbXLUNvOn+IQWVqUPN31iUWiwCPo0m0laRCFCUMxaDlaNInLhD4nrMr9EMAy1JDJCHkffseE/5q1LKMsnF7LDx6hB2tTIzQA9kXqLvIAbAjbL+jYLry1y+r/UrTmisOY+Tdhp0E79WiKWRO9IbzEzFTShC34JsRyycQXlgyEw92cz8WAEZqL1YDxQkpX+DK0j9hCGeycCTcR3FJFIA34m2+euPUYSpuemleds70HTnqdOvhthYPBotipMmdx8SS6mXIjKNY14YagadNX1rXxIKGzzC4FD6inqnmlOZfcg/JvJH9BEEVlKfIyRIw5PBo/3EjF8lL40urDOuM9MhMyJobfjR6NUfdVZtauXR8QYumtQ6X80d7eD6aKfPSB4L+eAQhE0yrDBXnCVk1Z5DHtn4+kd2ewvzff8+Oqg8CJjdoNof7dxpxUFTs7KrTepLIwqhdeLi87wEfE5IN62WXmFqwy2cY7xQ2O795YIke7YCm8BJ3C73i"; content = URLEncoder.encode(content,"utf-8"); String delalUrl ="http://ip地址/energy/dealData?content="+ content +"&unitCode="+unitCode; System.out.println(sendRequest(delalUrl,"POST"));// String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLz2yI+4AnRDJrKLQxb4kKKv2p5DW+u1iH60Xv+SaVYMADQAdKekBqPMAXWd/CHNqS1HrmtXuojgzHFP3tmAFk5ipvTcPL4RklXulcqQ4xaz8S+cnMBQTbVhgfZhJOGuum6CDnP3+6OEXbfuF9A7dYQvJo+Dho8u+G60qfX2DFFQIDAQAB";// publicKey = URLEncoder.encode(publicKey,"utf-8");//// String uploadKeyUrl = "http://ip地址/energy/uploadPublicKey?publicKey=" + publicKey + "&unitCode=" + unitCode;//// System.out.println(sendRequest(uploadKeyUrl, "POST"));//// String checkUrl ="http://ip地址/energy/getPublicKey?unitCode="+unitCode;// System.out.println(sendRequest(checkUrl,"GET")); }}