java发送HTTP和HTTPS请求

时间:2021-10-12 21:10:49

HTTP请求没什么好说的,如果您将请求HTTPS请求,在开启链接前请先绕过SSL验证,如下为绕过验证的工具类

package com.weavernorth.httpconn;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/** * 在请求HTTPS协议的URL遇到SSL证书验证不通过时可以借助此SslUtils绕过SSL验证 * @author Dylan * */
public class SslUtils {

    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    static class miTM implements TrustManager,X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }

    /** * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用 * @throws Exception */
    public static void ignoreSsl() throws Exception{
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

请求HTTP

package com.weavernorth.httpconn;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.weavernorth.util.FileUtil;
/** * 上传附件到泛微系统 * @author Dylan * */
public class HttpURLConnrctionToFile {

    public static String uploadFile(String uploadUrl, String file) {  
        byte[] bbyte = FileUtil.File2byte(file);
        String end = "\r\n"; 
        String twoHyphens = "--"; 
        String boundary = "---------------------------Dylan"; 
        try {  
            URL url = new URL(uploadUrl); 
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
            httpURLConnection.setDoInput(true); 
            httpURLConnection.setDoOutput(true); 
            httpURLConnection.setUseCaches(false); 
            httpURLConnection.setRequestMethod("POST"); 
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); 
            httpURLConnection.setRequestProperty("Charset", "UTF-8"); 
            httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 

            String name = new File(file).getName();
            DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream()); 
            dos.writeBytes(twoHyphens + boundary + end); 
            dos.writeBytes("Content-Disposition: form-data; name=\"file1\"; filename=\""+new String(name.getBytes("UTF-8"), "ISO_8859_1")+"\"" + end); 
            dos.writeBytes("Content-Type: application/octet-stream;" + end);
            dos.writeBytes(end); 
            dos.write(bbyte); 
            dos.writeBytes(end); 
            dos.writeBytes(twoHyphens + boundary + twoHyphens + end); 
            dos.flush(); 

            // 读取服务器返回结果  
            InputStream is = httpURLConnection.getInputStream(); 
            InputStreamReader isr = new InputStreamReader(is, "utf-8"); 
            BufferedReader br = new BufferedReader(isr); 
            String result = br.readLine(); 
            System.out.println("response" + result); 
            is.close(); 
            return  result; 

        } catch (Exception e) {  
            e.printStackTrace(); 
        }  
        return ""; 
    }



}

请求HTTPS

package com.weavernorth.httpconn;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.weavernorth.util.FileUtil;
import sun.misc.BASE64Decoder;
import weaver.conn.RecordSet;
import weaver.general.Util;

/** * 利用 HttpsURLConnection发送 HTTP或者HTTPS请求 * 若涉及SSL证书验证,必须在openConnection之前绕过验证 * 否则就会出现那个著名的异常No subject alternative names matching IP address ***.**.*.*** found * 请将请求头的设置放在写出缓存数据之前,否则会提示连接提前终止的异常 * @author Dylan */
public class HttpsURLConnectionUtil {

    public static StringBuffer doPut(String param,String filed) throws IOException, JSONException {  
        //查询邮件模块附件地址
        RecordSet rs = new RecordSet();
        //真实路径
        String strFilerealpath = "";
        //查询邮件附件相关信息
        rs.executeSql("select filename,filerealpath,filesize from mailresourcefile where id = '" + filed + "'");
        if(rs.next()){
            strFilerealpath = Util.null2String(rs.getString("filerealpath"));
        }
        // 数据处理 转换JSON对象
        JSONObject jsonParams;
        JSONArray arrDetail = null;
        try {
            jsonParams = new JSONObject(param);
            //strAuthrequest为HTTP的PUT请求中的请求协议配置项,为数组类型
            String strAuthrequest = jsonParams.getString("authrequest");
            //请求协议配置项
            arrDetail = new JSONArray(strAuthrequest);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        //绕过SSL验证
        try {
            SslUtils.ignoreSsl();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        //存储响应数据
        StringBuffer sbuffer=null;
        System.out.println(arrDetail.getString(1));
        URL uri = new URL(arrDetail.getString(1));  
        HttpsURLConnection conn = (HttpsURLConnection) uri.openConnection(); 
        conn.setDoInput(true);// 允许输入 
        conn.setDoOutput(true);// 允许输出 
        conn.setUseCaches(false); // 不允许使用缓存 
        conn.setRequestMethod(arrDetail.getString(0));//设置请求方式
        if (arrDetail != null) {
            for (int i = 2; i < arrDetail.length(); i++) {
                try {
                    String strVal = arrDetail.getString(i);
                    String[] split = strVal.split(":");
                    String substring = strVal.substring(strVal.indexOf(split[0]) + split[0].length() + 2, strVal.length());
                    //设置请求头
                    conn.setRequestProperty(split[0], substring);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("请求头设置完毕");

        }
        conn.setReadTimeout(10000);//设置读取超时时间 
        conn.setConnectTimeout(10000);//设置连接超时时间 
        conn.connect();
        OutputStream out = conn.getOutputStream();//向对象输出流写出数据,这些数据将存到内存缓冲区中 
        File file = new File(strFilerealpath);
        //判断文件是否存在
        if (file.exists()) {
            System.out.println("上传云盘的文件存在!");
            out.write(FileUtil.File2byte(strFilerealpath));
            out.flush();
            // 关闭流对象,此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中
            out.close();
            System.out.println("写出完毕");
        }else{
            System.out.println("需要上传云盘的文件不存在!");
        }
        sbuffer = new StringBuffer(conn.getResponseCode() + "");
        // 读取响应
        // if (conn.getResponseCode()==200){
        // 从服务器获得一个输入流
        InputStreamReader inputStream = new InputStreamReader(
                conn.getInputStream());// 调用HttpURLConnection连接对象的getInputStream()函数,
                                        // 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
        BufferedReader reader = new BufferedReader(inputStream);
        String lines;
        sbuffer = new StringBuffer("");
        while ((lines = reader.readLine()) != null) {
            lines = new String(lines.getBytes(), "UTF-8");
            sbuffer.append(lines);
        }
        reader.close();
        System.out.println(sbuffer);
        // 断开连接
        conn.disconnect();

        return sbuffer;    



    }  
    // base64解密
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}