2013-10-22 58 views
0

如何發送請求標頭?假設我想向網站發送HTTP請求時發送自己的請求標頭。如何發送請求標題?

我該怎麼做?

URL url = new URL(urlString); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

// HOW DO I SEND REQUEST HEADER LIKE Date,Cookie,Cache-Control ??? 
+2

http://docs.oracle.com/javase/7/docs/api/java/net/ URLConnection.html#setRequestProperty(java.lang.String,java.lang.String) – tom

回答

2
connection.setRequestProperty("HeaderName", "Value"); 
2

獲得HttpURLConnection的連接後,你可以做如下

//adding headers 
con.setRequestProperty("headername", value);