Dr. Danger
Dr. Danger

Reputation: 93

Getting HTML Source using Jsoup of a password protected website

As the title says, I am trying to use Jsoup, specifically the method

String html = Jsoup.connect(page.getUrl()).get().html(); 

The website is protected by username and password, and I have the login credentials but if I try to fetch the source of the page, the Url redirects to a "please login" page. I do not own the website (do not have direct access to database). Furthermore, I do not know http or Javascript. In the activity, the user will navigate in a webview and log-in to this website and once they are logged in, I get the URL and try to get the source (using the above method).

In summary, I can get the source successfully with Jsoup, but the URL redirects to a login page.

Thank you in advance.

Upvotes: 2

Views: 4565

Answers (1)

RanRag
RanRag

Reputation: 49567

You have to login to the website using your java code. Use live http header[firefox addon] to see all http headers and try to send these headers using your java code, so that the website thinks that a web browser is trying to connect to it.

In short, try to emulate the browser's behaviour and actions using your java code.

You can login using Jsoup from the following code:

Document doc = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername")
.data("password", "myPassword")
.post();

and then try to read the html of the page

Upvotes: 3

Related Questions