Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 显示实际原始数据+;HttpsURLConnection发送的HTTPS标头_Android - Fatal编程技术网

Android 显示实际原始数据+;HttpsURLConnection发送的HTTPS标头

Android 显示实际原始数据+;HttpsURLConnection发送的HTTPS标头,android,Android,我有以下连接到主机的简单代码: URL url = new URL("https://www.example.com/getResource"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); m_HttpsConnecti

我有以下连接到主机的简单代码:

URL url = new URL("https://www.example.com/getResource");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
m_HttpsConnection.setDoOutput(true);
OutputStream os = m_HttpsConnection.getOutputStream();
DataOutputStream wr = new DataOutputStream(os);
wr.writeBytes("{\"key\":\"value\"}");
wr.flush();
wr.close();

Log.d("MyActivity", "http raw data: " + conn.toString()); <-- this is what I want to achieve!

我该怎么做?谢谢

对于
OkHttpClient
HttpsURLConnection
,无法检查发送到主机的完整原始请求HTTP数据包

对于
OkHttpClient
,这是检查标题的最佳方法:

class LoggingInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();

        logger.info(String.format("Sending request %s",
                request.headers().toString()));

        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        logger.info(String.format("Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers()));

        return response;
    }
}
请注意,当您没有设置任何标题(
Request.Builder.header()
)时,日志中将不会显示任何内容。同样的观察结果适用于
HttpsURLConnection
HttpsURLConnection.getRequestProperties()
将仅显示您在
HttpsConnection.setRequestProperty()
中设置的内容

对于HTTP响应,情况就不同了。您可以获得整个原始响应头

对于
HttpsURLConnection
,您可以使用以下代码:

Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
Set<String> keys = map.keySet();

Log.d(TAG, "https response headers: ");
for (String key : keys) {
    List<String> list = map.get(key);
    for (String value : list) {
        Log.d(TAG, "key: " + key + ", value: " + value);
    }
}

我相信您也可以获得
OkHttpClient
的完整响应包。我没有检查

也许切换到OkHttp并添加日志拦截器。我正在从安全设备发送此消息。所以我有点不愿意使用第三方应用程序来处理我的数据。OkHttp不是一个应用程序。这是一个图书馆,我指的是第三方图书馆。无论如何,我会试试看。谢谢我无法使用OkHttp获取完整的原始请求头。但是,我能够获得响应标题的所有内容。我把我的测试结果作为答案。谢谢
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
Set<String> keys = map.keySet();

Log.d(TAG, "https response headers: ");
for (String key : keys) {
    List<String> list = map.get(key);
    for (String value : list) {
        Log.d(TAG, "key: " + key + ", value: " + value);
    }
}
D/MyActivity: key: null, value: HTTP/1.1 200 OK
D/MyActivity: key: Cache-Control, value: private
D/MyActivity: key: Content-Length, value: 20
D/MyActivity: key: Content-Type, value: application/json
D/MyActivity: key: Date, value: Thu, 22 Aug 2019 09:16:06 GMT
D/MyActivity: key: Server, value: Microsoft-IIS/10.0