如何在 Java 中使用 HTTP GET 请求?
How to use HTTP GET Requests in Java?
我正在尝试将现有 python 脚本重新编码为 Java。
它包括以下行:
r = requests.get('https://{}/redfish/v1/{}'.format(ip, query), auth=('ADMIN', 'ADMIN'), verify=False)
我在Python方面经验不多,也没有自己编写脚本。到目前为止,我只能弄清楚它的作用,但不知道如何使用 Java.
复制它
如果有人能给我指出正确的方向,那就太棒了。
谢谢!
首先,阅读this tutorial on the java HTTP client。 (注意需要jdk11以上)
从那里开始应该相当简单; .format()
只是用提供的 ip 和查询部分替换 {}
。授权部分更有趣。验证部分大概意味着 'whatever, forget about SSL'.
在密码 'admin' 和 'disregard SSL issues' 之间,这段代码尖叫着“你大约 2 周后就会收到你的盒子”,也许你应该比这更认真地对待安全问题.
无论如何,java 领域的等价物更复杂,因为 java 故意并不意味着 'disable ssl' 是随意的一次性举动,不像 python 不问任何问题,直接把火箭筒递给你。
Here is a tutorial on how to do basic http auth with the http client.
为了正确地把你的脚踢开并确保脚完全死了,你需要制作一个什么都不做的 SSL 上下文,并且静默地接受所有证书,即使是那些试图破解你的系统的人。然后将 .sslContext
的那个传递给 HttpClient.builder()
.
首先,您可以使用String.format
进行格式化:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
如果需要,您也可以使用 MessageFormat
。
为了连接,您可以创建一个 URL
对象并创建一个 URLConnection
(在您的情况下为 HttpsURLConnection
),然后为响应打开一个 InputStream
:
HttpsURLConnectioncon=(HttpsURLConnection)new URL(url).openConnection();
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}
为了做身份验证,你可以看看this tutorial:
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
//Get the HttpURLConnection
con.setRequestProperty("Authorization", authHeaderValue);
//Connect/open InputStream
如果您真的想禁用验证,您可以创建自己的 HostnameVerifier
允许所有内容,但强烈建议不要这样做,因为这 允许中间人攻击 作为你基本上 禁用 HTTPs 的安全性:
con.setHostnameVerifier((hostname,sslSession)->true);
全部组合起来,它可能看起来像这样:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
HttpsURLConnection con=(HttpsURLConnection)new URL(url).openConnection();
con.setRequestProperty("Authorization", authHeaderValue);
con.setHostnameVerifier((hostname,sslSession)->true);//vulnerable to man in the middle attacks
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}
我正在尝试将现有 python 脚本重新编码为 Java。 它包括以下行:
r = requests.get('https://{}/redfish/v1/{}'.format(ip, query), auth=('ADMIN', 'ADMIN'), verify=False)
我在Python方面经验不多,也没有自己编写脚本。到目前为止,我只能弄清楚它的作用,但不知道如何使用 Java.
复制它如果有人能给我指出正确的方向,那就太棒了。
谢谢!
首先,阅读this tutorial on the java HTTP client。 (注意需要jdk11以上)
从那里开始应该相当简单; .format()
只是用提供的 ip 和查询部分替换 {}
。授权部分更有趣。验证部分大概意味着 'whatever, forget about SSL'.
在密码 'admin' 和 'disregard SSL issues' 之间,这段代码尖叫着“你大约 2 周后就会收到你的盒子”,也许你应该比这更认真地对待安全问题.
无论如何,java 领域的等价物更复杂,因为 java 故意并不意味着 'disable ssl' 是随意的一次性举动,不像 python 不问任何问题,直接把火箭筒递给你。
Here is a tutorial on how to do basic http auth with the http client.
为了正确地把你的脚踢开并确保脚完全死了,你需要制作一个什么都不做的 SSL 上下文,并且静默地接受所有证书,即使是那些试图破解你的系统的人。然后将 .sslContext
的那个传递给 HttpClient.builder()
.
首先,您可以使用String.format
进行格式化:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
如果需要,您也可以使用 MessageFormat
。
为了连接,您可以创建一个 URL
对象并创建一个 URLConnection
(在您的情况下为 HttpsURLConnection
),然后为响应打开一个 InputStream
:
HttpsURLConnectioncon=(HttpsURLConnection)new URL(url).openConnection();
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}
为了做身份验证,你可以看看this tutorial:
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
//Get the HttpURLConnection
con.setRequestProperty("Authorization", authHeaderValue);
//Connect/open InputStream
如果您真的想禁用验证,您可以创建自己的 HostnameVerifier
允许所有内容,但强烈建议不要这样做,因为这 允许中间人攻击 作为你基本上 禁用 HTTPs 的安全性:
con.setHostnameVerifier((hostname,sslSession)->true);
全部组合起来,它可能看起来像这样:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
HttpsURLConnection con=(HttpsURLConnection)new URL(url).openConnection();
con.setRequestProperty("Authorization", authHeaderValue);
con.setHostnameVerifier((hostname,sslSession)->true);//vulnerable to man in the middle attacks
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}