我正在发出http GET请求以获取HTML源代码,但是在设置Accept Enocding属性时失败

时间:2019-06-08 15:24:16

标签: java http networking get http-post

我在网页上发出http get请求,并且还尝试对网页的html源代码进行处理,当我设置Accept Encoding的requestproperty时,我无法获取该页面的html源代码

当我注释掉该行时,我能够获取html源代码,但再次对其取消注释就失败了,并给出了类似代码``ʋ�z>。՗�v�AtFʊ�@Y�x�)3! ��������N>�M^��$J��h���@�88''

try {

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("Host","www.instagram.com");
            urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0");
            urlConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            urlConnection.setRequestProperty("Accept-Language","en-US,en;q=0.5");
            urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            urlConnection.setRequestProperty("Connection", "keep-alive");
            urlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
            urlConnection.connect();

            if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputstream = urlConnection.getInputStream();

             //   String response = getresponse(inputstream);
              //  System.out.println(response);

           String s = toString(inputstream);


private static String toString(InputStream inputStream) throws Exception
    {

        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")))
        {
            String inputLine;
            StringBuilder stringBuilder = new StringBuilder();
            while ((inputLine = bufferedReader.readLine()) != null)
            {
                stringBuilder.append(inputLine);
            }

            return stringBuilder.toString();
        }
    }

预期结果应类似于html源代码,但实际结果为 “ʋ�z>�՗�v�AtFʊ�@Y�x�)��3!���''�N>�M^��$J��h���@�88''

0 个答案:

没有答案