-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathHttpsUrls.java
More file actions
35 lines (34 loc) · 929 Bytes
/
HttpsUrls.java
File metadata and controls
35 lines (34 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void main(String[] args) {
{
try {
String protocol = "http://";
URL u = new URL(protocol + "www.secret.example.org/");
// BAD: This causes a 'ClassCastException' at runtime, because the
// HTTP URL cannot be used to make an 'HttpsURLConnection',
// which enforces SSL.
HttpsURLConnection hu = (HttpsURLConnection) u.openConnection();
hu.setRequestMethod("PUT");
hu.connect();
OutputStream os = hu.getOutputStream();
hu.disconnect();
}
catch (IOException e) {
// fail
}
}
{
try {
String protocol = "https://";
URL u = new URL(protocol + "www.secret.example.org/");
// GOOD: Opening a connection to a URL using HTTPS enforces SSL.
HttpsURLConnection hu = (HttpsURLConnection) u.openConnection();
hu.setRequestMethod("PUT");
hu.connect();
OutputStream os = hu.getOutputStream();
hu.disconnect();
}
catch (IOException e) {
// fail
}
}
}