/ / 302 Rediriger de http vers https dans Android à l'aide de Dropbox Short Hyperlinks - android, redirect, hyperlink, dropbox, short

302 Rediriger de http à https dans Android avec Dropbox Hyperliens courts - Android, redirection, hyperlien, dropbox, court

J'ai une application qui charge et joue de la musique à partir du stockage en nuage sans problème dans des circonstances normales, sauf lorsqu'il s'agit de Dropbox liens courts. Ces liens utilisent un en-tête 302 pour rediriger vers un lien https :

302 Trouvé La ressource a été trouvée à https://www.dropbox.com/s/jhzh3woy3qxblck/05%20-%20Cinema.ogg; toi devrait être redirigé automatiquement.

La structure de ce code est une double méthode statique, d'abord [chronologiquement] pour rechercher une redirection et ensuite pour transférer les données dans un fichier.

J'essaie de le faire fonctionner car le deuxième lien me permet actuellement de télécharger beaucoup de HTML non pertinent à partir de Dropbox, plutôt que le fichier requis lui-même !

/**
* 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;
}

Réponses:

3 pour la réponse № 1

J'ai rencontré le même problème. J'ai trouvé que je peux obtenir une nouvelle URL à partir de urlConnection.getHeaderField("Emplacement"); Veuillez vous référer au code ci-dessous.

{
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");
}
}

Salutations, Jack