簡體   English   中英

為什么URLConnection.getInputStream()不包含HTTP響應標頭?

[英]Why does URLConnection.getInputStream() not include the HTTP response headers?

我用這個

public static final String LOGIN_URL = "http://www.icourse163.org/passport/reg/icourseLogin.do";

public static void loginSimulation() throws IOException {
    URL loginUrl = new URL(LOGIN_URL);
    URLConnection connection = loginUrl.openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
    connection.setRequestProperty("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
    connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    connection.setRequestProperty("Connection", "keep-alive");
    connection.setRequestProperty("Referer", "http://www.icourse163.org/member/logout.htm");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setDoOutput(true);

    PrintWriter loginDataWriter = new PrintWriter(connection.getOutputStream());

    String loginData = "returnUrl=aHR0cDovL2ljb3Vyc2UxNjMub3JnLw%3D%3D&failUrl=aHR0cDovL3d3dy5pY291cnNlMTYzLm9yZy9tZW1iZXIvbG9naW4uaHRtP2VtYWlsRW5jb2RlZD1PVGM1TnpJME9EZ3lRSEZ4TG1OdmJRPT0%3D&savelogin=false&oauthType=&username=979724882%40qq.com";
    loginDataWriter.print(loginData);
    loginDataWriter.flush();



    BufferedReader reader = new BufferedReader(
            new InputStreamReader(connection.getInputStream()));
    StringBuilder builder = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null){
        builder.append(line);
    }

但是builder.toString沒有有關響應頭的任何信息嗎? 我認為我的客戶端和服務器之間有一條管道。 因此,所有響應都應通過此管道(包括響應標頭,響應內容等)。 但是結果不是。 為什么?

無論connection的類型是什么( HttpUrlConnectionURLConnection等),它將為標頭提供訪問器。 看一下名為*.eaders.* ,例如: getHeaderFields()

相比之下, getInputStream()可能只會讀取響應主體。

編輯1 :基於更新的問題,很明顯,您正在使用java.net.URLConnection所以我的原始答案是這樣的: getInputStream()僅讀取響應正文,您將從getHeaderFields()獲取HTTP響應標頭。

修改后的問題還明確表明,您不僅對如何獲取標頭感興趣,而且對為什么 InputStream中不提供標頭感興趣。 您的客戶端和原始HTTP響應之間有一個庫,該庫決定以這種方式向您呈現原始響應:(1)響應主體作為InputStream (2)標頭為Map<String,List<String>> 那是在實現該庫時做出的選擇。 因此,只要使用java.net.URLConnection這就是必須使用響應的方式。 FWIW,其他庫(例如Apache Commons的HttpClient)也做同樣的事情。

您應該使用getHeaderFields獲得響應頭。

您可以這樣嘗試嗎?

import java.net.*;
import java.io.*;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {
    URL oracle = new URL("http://www.oracle.com/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM