无法更改Java中的Host等标头信息

时间:2014-08-14 10:14:21

标签: java header host

我想更改java中的标头。 我需要更改主机名。 但是获得默认主机。

我的代码:

public static void main(String[] args) throws Exception {
    // TODO code application logic here
    URL oracle = new URL("http://localhost/header_received/");
    URLConnection yc = oracle.openConnection();
    System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
    yc.setRequestProperty ("Authorization", "Hello World");
    yc.setRequestProperty ("Host", "entuser");

    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
}

Out put:

<br/>Authorization: Hello World
<br/>User-Agent: Java/1.7.0_21
<br/>Host: **localhost**
<br/>Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
<br/>Connection: keep-alive
BUILD SUCCESSFUL (total time: 0 seconds)

1 个答案:

答案 0 :(得分:0)

尝试在HttpsURLConnection中转换连接。 这是一个为你工作的例子:

https://www.blackbaud.com/files/support/guides/infinitydevguide/Subsystems/inwebapi-developer-help/Content/Reference/Java/rfcosaHttpURLConnectionTechniqueA.htm

HttpsURLConnection yc =(HttpsURLConnection)oracle.openConnection();