-3

I have some url and i need to download file from it.

In some cases i got 301 redirect, and in some cases it redirects me to HTTPS url (for example, this is real http url that redirects to https: http://bit.ly/1laNsED)

I don't know in advance what i will get from this url - actual file, 301 redirect or 301 redirect to HTTPS.

How i can correctly handle this situation in Android?

UPDATE: Solved this issue with OkHttp.

Evgeny O
  • 1
  • 3

2 Answers2

0
 private static String readUrl(String urlString) throws Exception {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);

            return buffer.toString();
        } finally {
            if (reader != null)
                reader.close();
        }
    }

==============================================

JSONObject json = new JSONObject(readUrl("https://"+Globals._Gateway+"/Android/Service.svc/GetFile/"+Param));
0

You should use, HttpsURLConnection for https, check below link, http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html

Android: HTTPS (SSL) connection using HttpsURLConnection

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24