http://www.w3.org/2000/svg" style="display: none;"> 复制即用
package utils ; import lombok. extern. slf4j. Slf4j ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Component ; import javax. net. ssl . * ;
import java. io. BufferedReader ;
import java. io. IOException ;
import java. io. InputStreamReader ;
import java. io. PrintWriter ;
import java. net. HttpURLConnection ;
import java. net. URL ;
import java. net. URLConnection ;
import java. security. cert. X509Certificate ;
import java. util. List ; @Slf4j
@Component
public class HttpSendUtils { public static String sendHeaderPostWb ( String url, String contentType, String param, List < String > headers) { PrintWriter out = null ; BufferedReader in = null ; String result = "" ; HttpURLConnection conn = null ; try { URL realUrl = new URL ( url) ; boolean isHttps = "http s" . equalsIgnoreCase ( realUrl. getProtocol ( ) ) ; if ( isHttps) { SSLContext ssl Context = SSLContext . getInstance ( "TLS" ) ; ssl Context. init ( null , new X509TrustManager [ ] { new X509TrustManager ( ) { @Override public X509Certificate [ ] getAcceptedIssuers ( ) { return null ; } @Override public void checkClientTrusted ( X509Certificate [ ] certs, String authType) { } @Override public void checkServerTrusted ( X509Certificate [ ] certs, String authType) { } } } , new java. security. SecureRandom( ) ) ; SSLContext . setDefault ( ssl Context) ; HostnameVerifier hv = new HostnameVerifier ( ) { @Override public boolean verify ( String hostname, SSLSession session) { return true ; } } ; conn = ( HttpsURLConnection ) realUrl. openConnection ( ) ; ( ( HttpsURLConnection ) conn) . setHostnameVerifier ( hv) ; } else { conn = ( HttpURLConnection ) realUrl. openConnection ( ) ; } conn. setRequestProperty ( "Content-Type" , contentType) ; conn. setRequestProperty ( "accept" , "*/*" ) ; conn. setRequestProperty ( "connection" , "Keep-Alive" ) ; conn. setRequestProperty ( "Accept-Charset" , "UTF-8" ) ; conn. setRequestProperty ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" ) ; if ( ! headers. isEmpty ( ) ) { for ( String header : headers) { String [ ] parts = header. split ( ":" ) ; conn. setRequestProperty ( parts[ 0 ] . trim ( ) , parts[ 1 ] . trim ( ) ) ; } } conn. setDoOutput ( true ) ; conn. setDoInput ( true ) ; out = new PrintWriter ( conn. getOutputStream ( ) ) ; out. print ( param) ; out. flush ( ) ; in = new BufferedReader ( new InputStreamReader ( conn. getInputStream ( ) , "utf-8" ) ) ; String line; while ( ( line = in. readLine ( ) ) != null ) { result += line; } } catch ( Exception e) { e. printStackTrace ( ) ; } finally { try { if ( out != null ) { out. close ( ) ; } if ( in != null ) { in. close ( ) ; } } catch ( IOException ex) { ex. printStackTrace ( ) ; } } return result; }
}