Note that there are some explanatory texts on larger screens.

plurals
  1. POfollowing HTTPS redirects with HttpURLConnection
    primarykey
    data
    text
    <p>I have a problem when my application make a HTTPS request that redirect to another HTTPS url.</p> <p>With the HTTP to HTTP redirection I have no problem.</p> <p>Before that I have used HttpPost and HttpGet requests with any redirecting problem, but in the way as I do it I did not upload files bigger than 2 MB.</p> <p>The code I wrote:</p> <pre><code> public Bundle getResponseFromHttpRequest(Bundle requestBundle) { /* * TO DO: httpRequest (bundle): headers: bundle params: bundle url: String method: {GET, POST} */ String urlString = requestBundle.getString("url"); String method = requestBundle.getString("method"); Bundle headersBundle = requestBundle.getBundle("headers"); Bundle paramsBundle = requestBundle.getBundle("params"); Bundle responseBundle = new Bundle(); try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); if (headersBundle != null) { Set&lt;String&gt; nameHeaders = headersBundle.keySet(); for (String nameHeader : nameHeaders) { String valueHeader = headersBundle.getString(nameHeader); connection.setRequestProperty(nameHeader, valueHeader); } //selectRequestFromMethod(method, getRequest, postRequest).setParams(params); } HttpsURLConnection.setFollowRedirects(true); HttpURLConnection.setFollowRedirects(true); connection.setInstanceFollowRedirects(true); if (paramsBundle != null) { Set&lt;String&gt; nameParams = paramsBundle.keySet(); ByteArrayOutputStream baosParams = new ByteArrayOutputStream(); for (String nameParam : nameParams) { baosParams.write(URLEncoder.encode("&amp;",HTTP.UTF_8).getBytes(HTTP.UTF_8)); baosParams.write(URLEncoder.encode(nameParam,HTTP.UTF_8).getBytes(HTTP.UTF_8)); baosParams.write(URLEncoder.encode("=",HTTP.UTF_8).getBytes(HTTP.UTF_8)); baosParams.write(URLEncoder.encode(paramsBundle.getString(nameParam),HTTP.UTF_8).getBytes(HTTP.UTF_8)); } if (method.equals("POST")) { //HttpEntity byteArrayEntity = new ByteArrayEntity(baosParams.toByteArray()); //postRequest.setEntity(byteArrayEntity); connection.setRequestMethod("POST"); connection.setDoOutput(true); //pra poder enviar datos: petición POST. connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length",""+baosParams.size()); connection.setRequestProperty("Accept-Encoding", "identity"); connection.setFixedLengthStreamingMode(baosParams.size()); OutputStream outputStream = connection.getOutputStream(); outputStream.write(baosParams.toByteArray()); outputStream.close(); connection.disconnect(); Map&lt;String, List&lt;String&gt;&gt; headers = connection.getHeaderFields(); Set&lt;String&gt; headerNames = headers.keySet(); Bundle responseHeadersBundle = new Bundle(); for (String headerName : headerNames) { List&lt;String&gt; headerValues = headers.get(headerName); String [] headerValuesStr = new String[headerValues.size()]; headerValues.toArray(headerValuesStr); responseHeadersBundle.putStringArray(headerName, headerValuesStr); } responseBundle.putBundle("headers", responseHeadersBundle); Log.v("HTTP UTIL CONNECTION","RESPONSE CODE: " + connection.getResponseCode()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder content = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); } bufferedReader.close(); String contentString = content.toString(); responseBundle.putString("content", contentString); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { Log.v("HTTP UTIL CONNECTION","response code 200"); } } else if (method.equals("GET")) { connection.setRequestMethod("GET"); connection.setDoOutput(false); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder content = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); } bufferedReader.close(); String contentString = content.toString(); responseBundle.putString("content", contentString); } else { Log.v("INVALID METHOD",method); return null; } } } catch (Exception e) { e.printStackTrace(); } return responseBundle; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload