PDA

View Full Version : Turn URL in to String?



Mat
10-24-2012, 11:11 AM
I can't find/figure out how to do this.
Any ideas?
Mat

riwu
10-24-2012, 11:19 AM
Isn't an URL already a string? If not what it is stored as? (i'm sure there is no such thing as 'URL data type')

Mat
10-24-2012, 11:23 AM
i mean the data, like how simba takes a url and turn the soruce in to a string.
Mat

Brandon
10-24-2012, 11:44 AM
i mean the data, like how simba takes a url and turn the soruce in to a string.
Mat

What? I would take a HUGE guess and say that you're asking how Simba's GetPage function works..

If so then the answer is "sockets" under the hood. Every form of data communication between you and the internet is through a socket.

When a URL gets parsed, it converts that string into the corresponding IP address that matches it.

For example, you can type in the IP of a website, and it'll take you directly to the home page rather than typing in the site's text address (ex: Google.com).

Anyway, you connect to the internet through a socket/port and do a GET or a POST to retrieve information. There's many other ways but these are the most used.

leaf
11-21-2012, 07:56 PM
URL#toString?

tls
11-21-2012, 08:29 PM
There are tons of methods out there already made, the idea is pretty simple if you know the java api.
You create the url object, use URL#openConnection(), set the connection to GET, use a bufferedreader on the connection. Ta-da.
If you don't get that then google: "get html page with java"

Wyn
12-01-2012, 08:10 AM
I believe it's


String site = new Scanner(new URL("Website").openStream(), "UTF-8").useDelimiter("\\A").next();

ShawnjohnSJ
01-07-2013, 05:06 AM
Easiest way IMO would be to use the Socket class to open a socket to the specific webpage then use a BufferedReader to read in the data. Theres probably streams other than BufferedReader that would be better to use for this though.

Pseudo-code:



public String getPage(String url) throws Exception { // can't remember all the exceptions you have to throw
Socket s = new Socket("http://www.google.com/", 80);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

String temp = null;
String data = null;
while((temp = in.readLine()) != null) {
data += temp;
}
in.close();
s.close();
return data;
}

Echo_
01-07-2013, 05:15 AM
Easiest way IMO would be to use the Socket class to open a socket to the specific webpage then use a BufferedReader to read in the data. Theres probably streams other than BufferedReader that would be better to use for this though.

Pseudo-code:



public String getPage(String url) throws Exception { // can't remember all the exceptions you have to throw
Socket s = new Socket("http://www.google.com/", 80);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

String temp = null;
String data = null;
while((temp = in.readLine()) != null) {
data += temp;
}
in.close();
s.close();
return data;
}


Appending to a String several times is inefficient, use a StringBuilder.

private String readPage(String address) throws IOException {
URL url = new URL(address);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String input;
StringBuilder output = new StringBuilder();
while ((input = br.readLine()) != null)
output.append(input).append("\n");

br.close();
return output.toString();
}

ShawnjohnSJ
01-07-2013, 05:44 AM
Appending to a String several times is inefficient, use a StringBuilder.

private String readPage(String address) throws IOException {
URL url = new URL(address);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String input;
StringBuilder output = new StringBuilder();
while ((input = br.readLine()) != null)
output.append(input).append("\n");

br.close();
return output.toString();
}
Like I said, it was just pseudo code. But thanks for correcting me.

MalikDz
02-08-2013, 06:23 AM
String.valueOf();

Echo_
02-08-2013, 07:05 PM
String.valueOf();

no.