开发者

Connect to site through https

开发者 https://www.devze.com 2022-12-12 04:00 出处:网络
I have some application that should connect to https Site, and receive some. With connection al开发者_开发知识库l is ok, but when i what getInputStream() comes Exception:

I have some application that should connect to https Site, and receive some. With connection al开发者_开发知识库l 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()));


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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消