I'm trying to make a command line web browser. But I'm not sure where to start.
Where should I start?
Printable View
I'm trying to make a command line web browser. But I'm not sure where to start.
Where should I start?
Look into java.net.HttpUrlConnection
Redirect is either an HTTP or HTML level command. The HTTP redirect code is 301, while in HTML, you useCode:<meta http-equiv="refresh" content="0; URL=http://website.to.redirect.to.com">
I have found out how to store cookies.
But when I try to login to a website it takes me back to the index page.
This is what I use to login:
Code:Cookies cm = new Cookies();
try {
String data = URLEncoder.encode("email", "utf-8") + "=" + URLEncoder.encode("email", "utf-8");
data += "&" + URLEncoder.encode("pass", "utf-8") + "=" + URLEncoder.encode("pass", "utf-8");
URL url = new URL("websitewhereilogin");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
cm.setCookies(conn);
conn.connect();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
cm.storeCookies(conn);
System.out.println(cm);
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
I wish it can be that simple to login a website.
Some websites have strict connection security. You have to get auth .....
You have to dig in these stuffs
But if you login a regular web page, Then you can get the web page content and parse to find neccessary login tags.
ex: <form ....... method='POST'>
<input type='text' value="" name="accountName".....>
<input type='password' value="" name="accPassword">
<input type='text' hidden....>
<button Submit>
</form>
//You have to know at least basic html to understand this.
Detect neccessary inputs first, like accountName, accPassword(There can be other things in hidden). Then form method is POST so it will be sent in invisible string.
you have to send URL+posted information to destination URL. ( & can not be used instead use &)
Use URLEncoder but don't divide the information
String URL=".............";
URLEncoder.encode(URL+"blalba=bla&aa=aa1","UTF-8");