本文介紹了在 URLConnection 中設置標頭的正確方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的代碼如下:
URLConnection cnx = address.openConnection();
cnx.setAllowUserInteraction(false);
cnx.setDoOutput(true);
cnx.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
InputStream is = cnx.getInputStream();
如果我在獲得 InputStream
之前設置標頭可以嗎?我的標頭會被發送,還是服務器會看到默認的 URLConnection
的用戶代理(如果有)?
Is it ok if I set the headers before I get the InputStream
? Will my header be sent, or will the server see the default URLConnection
's user-agent ( if any ) ?
推薦答案
在獲得 InputStream
之前必須設置標頭 以產生任何影響 - 如果連接已經打開,將拋出 IllegalStateException
.
The headers must be set prior to getting the InputStream
to have any affect - an IllegalStateException
will be thrown if the connection is already open.
具體到 User-Agent
標頭,如果已設置則應發送.
As far as the User-Agent
header specifically, it should be sent if it has been set.
請參閱 URLConnection JavaDoc.
See the URLConnection JavaDoc.
這篇關于在 URLConnection 中設置標頭的正確方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!