简体   繁体   中英

Decyrpting SSL traffic from OkHttp response body

I'm working on an application where I connect to a website with OkHttp , get the response data and extract the required data with Jsoup .

Everything is fine until I connect to an unsecured HTTP URL. When I connect to the HTTPS URL, the response is encoded and I've no idea how to decode it.

I went through several StackOverflow threads about this specific problem and how SSL works, but no benefit.

Here's a block of code I'm using:

private void startInternal() throws Exception {
    Request request = new Request.Builder()
            .url(att_link)
            .get()
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language", "en-US,en;q=0.5")
            .addHeader("Accept-Encoding", "gzip, deflate, br")
            .addHeader("Connection", "keep-alive")
            .addHeader("Referer", "https://www.abcdx.org/")
            .addHeader("Upgrade-Insecure-Requests", "1")
            .build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (Exception e) {
        runner.onError();
        e.printStackTrace();
    }
    if (response != null) {
        if (response.body() != null) {
            obtainCookieAndGo(Objects.requireNonNull(response.body()).string());
        } else {
            runner.onError();
        }
    } else {
        runner.onError();
    }
}

EXPECTED OUTPUT

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><meta charset="utf-8" /><title>
</title><link href="images/favicon.png" rel="shortcut icon" /><link href="css/dashboard.css" rel="stylesheet" type="text/css" media="all" /><link href="css/font-awesome.css" rel="stylesheet" type="text/css" media="all" /><link href="css/font-awesome.min.css" rel="stylesheet" type="text/css" media="all" /><link href="css/menu.css" rel="stylesheet" type="text/css" media="all" />
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/organictabs.jquery.js" type="text/javascript"></script>
    <script src="tinymce3.x/jscripts/tiny_mce/tiny_mce.js" type="text/javascript"></script>
</head>
<body>
    <form method="post" action="./student-attendance.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="CMjQBN==" />
</div>

ACTUAL OUTPUT

��������������`I�%&/m�{J�J��t�`$ؐ@������iG#)�*��eVe]fx@
�흼��{���{���;�N'���?\fdl��J�ɞ!���?~|?"~��7N��O�<y��
�<M���L_~����I���ݻ߽wr���7O����o�x��w�7u�l����Yy��鋏ҏ�m�zt�������޸�/�yu
`���u������GG�5��y6;z���,����>Z����w��E[����N���/�:OO�
�2��by�f�Y�E��.�E�lG��U���<k    �]y�qY,ߦ�:?��bA����e1�����⣴���>j�U�N�
m�Ϲc�i�e�|Re�lL����2o�y�~��׫�����]{�,�Y�}�QV�1h�ղ�ή�Z��<�E��F�E�_�o

This has nothing to do with SSL.

 .addHeader("Accept-Encoding", "gzip, deflate, br")

You explicity claim that you support various content encodings (compression) but then you don't handle these in your code. If you want to get a plain response just don't claim to support a compressed response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM