我想更改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)
答案 0 :(得分:0)
尝试在HttpsURLConnection中转换连接。 这是一个为你工作的例子:
HttpsURLConnection yc =(HttpsURLConnection)oracle.openConnection();