tags:

views:

87

answers:

3

I have some application that should connect to https Site, and receive some. With connection all is ok, but when i what getInputStream() comes Exception:

java.io.IOException: Server returned HTTP response code: 403 for URL:

Here is the part of code:

    String query = siteURL.toExternalForm();

 URL queryURL = new URL(query);

 String data = "username="+login+"&password="+password;

 URLConnection connection = queryURL.openConnection();

 connection.setDoOutput(true);

 OutputStreamWriter writer = new OutputStreamWriter(connection
   .getOutputStream());
 writer.write(data);
 writer.flush();

 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+1  A: 

Looks like you're not allowed to do what you're trying to do, you're getting an HTTP 403: Forbidden.

Can you open the same URL in your browser?

Ben S
Funny thing, that i can open it in the browserAnd i have found that, actually if I try to connection.connect();then variable connected is false...
Le_Coeur
+1  A: 

I'm very rusty on Java, but I think you need to use a HttpsURLConnection for secure connections rather than a URLConnection. This would explain your 403 Forbidden error - it's the same behaviour you get (or should get) when trying to access a secure web page without SSL encryption.

This looks like it might help.

Mark B
org.apache.commons.httpclient.HttpClient is very good also
Trick
You think wrong, or at least halfway wrong. queryURL.openConnection() returns a subclass of URLConnection depending on the protocol used in the URL, in this case an isntance of HttpsURLConnection, at least if query really starts with "http://...".
jarnbjo
I stand corrected - told you I was rusty :)
Mark B
+1  A: 

I think the site have a custom authentication mechanism, in wich you have to supply our username and password as GET parameters. So your url should look like this:

URL url = new URL("http://somesite.org/page?username=<username>&password=password");
... = url.openConnection();
...

If you use url.openConnection, a HTTP GET request is done. If you want to send data with a request, you must use a HTTP POST request. In this case, you can use a third party library, like Apache Commons HttpClient.

BTW: why are u creating a new URL object, if you already have one?

Salandur
It doesn't works((BTW: why are u creating a new URL object, if you already have one?>> I have only one URL object -> queryURL
Le_Coeur
Salandur is however probably right. You are making a GET request to the server, but try to write the request parameters in the request body, which is usually only supported with a POST request.
jarnbjo
@Le_Coeur: you must have an other one, since you are calling String query = siteURL.toExternalForm();
Salandur
I have erased some code from the beginnig, it was String query = siteURL.toExternalForm()+"/var/start";
Le_Coeur