2

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.

Dr. Danger
  • 93
  • 2
  • 7

1 Answers1

2

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

RanRag
  • 43,987
  • 34
  • 102
  • 155
  • what's the difference between the 'password' and 'myPassword'? Hopefully this works! :D – Dr. Danger Jan 01 '12 at 01:51
  • `myPassword` is the password you are using to login. – RanRag Jan 02 '12 at 06:51
  • and also see Jsoup documentation for reference [Jsoup](http://jsoup.org/cookbook/input/load-document-from-url) and http://jsoup.org/apidocs/org/jsoup/Connection.html#data(java.lang.String, java.lang.String) – RanRag Jan 02 '12 at 09:43