Java http請求獲取Location內容

一次對接三方外呼系統的時候,在解析對方返回的通話記錄的時候,其中錄音返回的是一個URL地址,可以通過瀏覽器訪問,然後跳轉到一個以.mp3結尾的地址去聽取錄音。

三方返回的錄音地址:

https://as01.nxcloud.com/record/83fe5634-a34c-7a0f-5db8-b1d45a162af0

經對三方提供的URL地址請求返回參數打印如下:

Keep-Alive--->[timeout=5, max=100]
null--->[HTTP/1.1 302 Found]
Server--->[Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16 mod_wsgi/4.6.2 Python/3.6]
Connection--->[Keep-Alive]
Content-Length--->[0]
Date--->[Sat, 15 Feb 2020 07:10:51 GMT]
Content-Type--->[text/html; charset=UTF-8]
Location--->[http://cs01.nxcloud.com:7180/1815001/349411f0-86dc-4df1-a4e3-d7ca6d6f1bc1.mp3]
X-Powered-By--->[PHP/5.4.16]
-1
-1
[B@24a35978

其實真實的錄音地址是在Location中,故首先得獲取該Location中的地址,然後在對Location地址解析。

工具類:

package com.github.collection.common.util;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;

public class HttpRequestUtil {

    public static void main(String[] args) {
        try {
            String url = "https://as01.nxcloud.com/record/83fe5634-a34c-7a0f-5db8-b1d45a162af0";
            String urlNameString = url;
            //忽略安全證書
            trustAllHosts();
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接
            URLConnection connection = realUrl.openConnection();
            // 設置通用的請求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setConnectTimeout(30 * 1000);
            // 建立實際的連接
            connection.connect();
            String location = connection.getHeaderField("Location");
            System.out.println(location);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void trustAllHosts() {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[]{};
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }
        }};
        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

之後可對該網路錄音轉換成byte[]


    /**
     * 讀取錄音文件流
     * @param url
     * @return
     * @throws Exception
     */
    public static byte[] readAndSave(String url)throws Exception{
        System.out.println("Starting.");
        URL u = new URL(url);
        byte[] buffer = new byte[1024*8];
        int read;
        int ava = 0;
        long start = System.currentTimeMillis();
        BufferedInputStream bin = new BufferedInputStream(u.openStream());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((read=bin.read(buffer))>-1){
            bos.write(buffer, 0, read);
            ava+=read;
            long speed = ava/(System.currentTimeMillis()-start);
            System.out.println("Download: "+ava+" byte(s)"+" avg speed: "+speed+" (kb/s)");
        }
        bos.flush();
        bos.close();
        System.out.println("Done. size:"+ava+" byte(s)");
        return bos.toByteArray();
    }

然後可以存進阿里雲OSS中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章