Reputation: 55
I'm trying to use this code to remove a user from a database before running some tests:
URL url = new URL("someurl/[email protected]");
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
String userPassword = "is:IS!";
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
huc.setRequestMethod("GET");
huc.setRequestProperty("Authorization", "Basic " + encoding);
huc.connect();
It doesn't work for me (user001
is still presented in database). But all works fine if I use .getResponseCode()
instead of .connect()
. Why?
Upvotes: 1
Views: 75
Reputation: 536
In the sun.net.www.protocol.http.HttpURLConnection implementation of HttpURLConnection, connect() by itself does not actually send the request (including authenticate, handle re-directs, etc.). That requires a call to getInputStream(), which may come later.
The call to getResponseCode calls getInputStream if has not been called already.
Upvotes: 1
Reputation: 1786
Because when you call
huc.connect();
you are not actually calling the url. You are just opening connection to the server. And normally you shouldn't call that method because that is called when you call something like
getInputStream(), getResponseCode(), or getResponseMessage()
Upvotes: 2