当我的应用程序发出重定向到另一个 HTTPS url 的 HTTPS 请求时,我遇到了问题。
使用 HTTP 到 HTTP 重定向我没有问题。
在此之前,我曾使用 HttpPost 和 HttpGet 请求解决任何重定向问题,但在这样做的过程中,我没有上传大于 2 MB 的文件。
我写的代码:
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<String> 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<String> nameParams = paramsBundle.keySet();
ByteArrayOutputStream baosParams = new ByteArrayOutputStream();
for (String nameParam : nameParams) {
baosParams.write(URLEncoder.encode("&",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<String, List<String>> headers = connection.getHeaderFields();
Set<String> headerNames = headers.keySet();
Bundle responseHeadersBundle = new Bundle();
for (String headerName : headerNames) {
List<String> 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;
}