Le_Coeur
Le_Coeur

Reputation: 1541

Connect to site through https

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()));

Upvotes: 1

Views: 759

Answers (2)

Salandur
Salandur

Reputation: 6453

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?

Upvotes: 1

Ben S
Ben S

Reputation: 69342

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?

Upvotes: 1

Related Questions