[英]HTTPS GET request using existing browser session in Java
我有一個用例,其中有一組受密碼保護的 HTTPS GET 請求,我想訪問請求正文,但每次都不需要傳遞用戶名/密碼,而是使用我的 Chrome 瀏覽器會話。
這是我每次提供我的憑據時創建新的 HTTP 連接的嘗試方式,這不值得推薦,而且速度也很慢。
String url = "<Request URL>";
URL obj = new URL(url);
String encoding = Base64.getEncoder().encodeToString(("<username>:<password>").getBytes());
System.out.println("Encoded Credential String:==>"+encoding);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.setRequestProperty ("Authorization", "Basic " + encoding);
System.out.println("Response connection string: "+con.toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
有人可以為我提供指導,如果我的實現是在 Java 中,我應該如何進行?
在任何有意義的意義上,使用基本身份驗證實際上並不是任何“慢”。
但是,如果您想使用 cookie,那么您可以這樣做
urlConn.setRequestProperty("Cookie", "cookiename=cookievalue");
而不是您當前擁有的Authorization
標頭。 這是假設您知道要發送的 cookie 名稱和值。 您可以檢查 Chrome HTTP 流量以獲取該信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.