302 Redirect from http to https in Android using Dropbox short Hyperlinks

醉酒当歌 提交于 2019-12-24 03:35:19

问题


I have an app which loads and plays music from cloud storage without problems in normal circumstances, except when it comes to Dropbox short links. These links use a 302 header to redirect to an https link:-

302 Found The resource was found at https://www.dropbox.com/s/jhzh3woy3qxblck/05%20-%20Cinema.ogg; you should be redirected automatically.

The structure of this code is a double static method, first [chronologically] to look for a redirect and second to get the data to a file.

I'm trying to get it to work because the second link currently makes me download a lot of irrelevant HTML from Dropbox, rather than the required file itself!

/**
 * Return an Audio File from a URL String
 * throws IOException
 * 
 * @param url the URL which provides the target
 * @return File - audio file
 */
public static File getAudioFile(String urlString, File f) {
    String newUrl = null;
    try {
        newUrl = getRedirect(urlString);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException Error getting Redirect ", e);
    } catch (IOException e) {
        Log.e(TAG, "IOException Error getting Redirect", e);
    }
    if (newUrl != null) {
        urlString = newUrl;
    }
    else {
        Log.i(TAG, "IOException Error getting Redirect because its null");
    }

    try {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setInstanceFollowRedirects(true);
        conn.setReadTimeout(40 * 1000);
        conn.setConnectTimeout(45 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "close");
        conn.setDoInput(true);
        conn.setDefaultUseCaches(true);
        // Starts the input from the URL
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream(f));

        // write the inputStream to the FileOutputStream
        byte [] bytes = new byte [409600];
        int block;
        while((block = bis.read(bytes, 0, bytes.length)) > 0) {
            bof.write(bytes, 0, block);
            Log.d(TAG, "Write a block of " + block);
        }

        bof.flush();
        bof.close();
        bis.close();
        is.close();
        conn.disconnect();
    } catch (Exception e) {
        Log.e(TAG, "Error getting Audio File", e);
    }
    return f;
}
/**
 * Get the new url from a http redirect 
 * throws IOException
 * 
 * @param url the URL which provides the target
 * @return url - the single url redirect
 */
public static String getRedirect(String urlString) throws ClientProtocolException, IOException {
    HttpParams httpParameters = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParameters, false);

    HttpClient httpClient = new DefaultHttpClient(httpParameters);      
    HttpGet httpget = new HttpGet(urlString);
    HttpContext context = new BasicHttpContext();

    HttpResponse response = httpClient.execute(httpget, context);

    // If we didn't get a '302 Found' we aren't being redirected.
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        throw new IOException(response.getStatusLine().toString());

    Header loc[] = response.getHeaders("Location");
    return loc.length > 0 ? loc[loc.length -1].getValue() : null;
}

回答1:


I met the same problem. I found that I can get new URL from urlConnection.getHeaderField("Location"); Please refer to below code.

{
    url = new URL(urlString);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.connect();
    String ResponseCode = urlConnection.getResponseCode();
    String ContentType = urlConnection.getContentType();
    if ( result.ResponseCode == HttpURLConnection.HTTP_MOVED_TEMP || result.ResponseCode == HttpURLConnection.HTTP_MOVED_PERM )
    {
       String Location = urlConnection.getHeaderField("Location");
    }
}

Regards, Jack



来源:https://stackoverflow.com/questions/14749307/302-redirect-from-http-to-https-in-android-using-dropbox-short-hyperlinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!