Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用HttpURLConnection执行以下HTTPS重定向_Android_Http_Https_Httpurlconnection - Fatal编程技术网

Android 使用HttpURLConnection执行以下HTTPS重定向

Android 使用HttpURLConnection执行以下HTTPS重定向,android,http,https,httpurlconnection,Android,Http,Https,Httpurlconnection,当我的应用程序发出重定向到另一个HTTPS url的HTTPS请求时,我遇到了一个问题 通过HTTP到HTTP重定向,我没有问题 在此之前,我使用过HttpPost和HttpGet请求,但在我这样做的过程中,我没有上传大于2MB的文件 我写的代码是: public Bundle getResponseFromHttpRequest(Bundle requestBundle) { /* * TO DO: httpRequest (bundle): hea

当我的应用程序发出重定向到另一个HTTPS url的HTTPS请求时,我遇到了一个问题

通过HTTP到HTTP重定向,我没有问题

在此之前,我使用过HttpPost和HttpGet请求,但在我这样做的过程中,我没有上传大于2MB的文件

我写的代码是:

    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;
}
公共捆绑包getResponseFromHttpRequest(捆绑包请求捆绑包){
/*
*要做:
httpRequest(bundle):
标题:bundle
参数:束
url:String
方法:{GET,POST}
*/
字符串urlString=requestBundle.getString(“url”);
String方法=requestBundle.getString(“方法”);
Bundle headersBundle=requestBundle.getBundle(“headers”);
Bundle paramsBundle=requestBundle.getBundle(“params”);
Bundle responseBundle=新Bundle();
试一试{
URL=新URL(URL字符串);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
if(headersBundle!=null){
Set nameHeaders=headersBundle.keySet();
for(字符串名称头:名称头){
String valueHeader=headersBundle.getString(nameHeader);
setRequestProperty(nameHeader、valueHeader);
}
//选择RequestFromMethod(method,getRequest,postRequest).setParams(params);
}
HttpsURLConnection.setFollowRedirects(true);
HttpURLConnection.setFollowRedirects(true);
connection.setInstanceFlowRedirects(true);
if(paramsBundle!=null){
Set nameParams=paramsBundle.keySet();
ByteArrayOutputStream=newbytearrayoutputstream();
for(字符串nameParam:nameparms){
write(URLEncoder.encode(“&”,HTTP.UTF_8).getBytes(HTTP.UTF_8));
write(URLEncoder.encode(nameParam,HTTP.UTF_8).getBytes(HTTP.UTF_8));
write(URLEncoder.encode(“=”,HTTP.UTF_8).getBytes(HTTP.UTF_8));
write(URLEncoder.encode(paramsBundle.getString(nameParam),HTTP.UTF_8.getBytes(HTTP.UTF_8));
}
if(方法等于(“POST”)){
//HttpEntity byteArrayEntity=新的byteArrayEntity(baoparams.toByteArray());
//setEntity(byteArrayEntity);
connection.setRequestMethod(“POST”);
connection.setDoOutput(true);//pra poder enviar datos:petición POST。
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
connection.setRequestProperty(“内容长度”,“参数大小());
setRequestProperty(“接受编码”、“标识”);
connection.setFixedLengthStreamingMode(参数.size());
OutputStream OutputStream=connection.getOutputStream();
outputStream.write(参数.toByteArray());
outputStream.close();
连接断开();
Map headers=connection.getHeaderFields();
Set headerNames=headers.keySet();
Bundle responseHeadersBundle=新Bundle();
用于(字符串头名称:头名称){
List headerValues=headers.get(headerName);
字符串[]headerValuesStr=新字符串[headerValues.size()];
headerValues.toArray(headerValuesStr);
putStringArray(headerName,headerValuesStr);
}
putBundle(“headers”,responseHeadersBundle);
Log.v(“HTTP-UTIL-CONNECTION”,“响应代码:”+CONNECTION.getResponseCode());
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
弦线;
StringBuilder内容=新建StringBuilder();
而((line=bufferedReader.readLine())!=null){
content.append(第+行“\n”);
}
bufferedReader.close();
字符串contentString=content.toString();
putString(“内容”,contentString);
if(connection.getResponseCode()==HttpURLConnection.HTTP\u确定){
Log.v(“HTTP UTIL连接”,“响应代码200”);
}
}
else if(method.equals(“GET”)){
connection.setRequestMethod(“GET”);
连接设置输出(假);
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
弦线;
StringBuilder内容=新建StringBuilder();
而((line=bufferedReader.readLine())!=null){
content.append(第+行“\n”);
}
bufferedReader.close();
字符串contentString=content.toString();
putString(“内容”,contentString);
}
否则{
Log.v(“无效方法”,方法);
返回null;
}
}
}
捕获(例外e){
e、 printStackTrace();
}
返回响应bundle;
}

您的问题到底是什么?你介意用适当的logcat跟踪进一步解释吗?对不起,我的问题是我想让我的代码遵循HTTPS重定向,但它不这样做。然而,我以前的实现遵循重定向没有问题,但我希望通过这种方式处理HTTPS请求,原因是其他原因。解决方法:我的代码在向GAS脚本发送请求后的行为。无论如何,谢谢你。