Read url into a string?

S

SpreadTooThin

I want to open a url and read it into a string.
That string will be passed onto a json parser.
I tried this.. but it's doing nothing...

import java.net;
import java.io.*;

function getURL(url) {
URL hp = new URL(url);
URLConnection yc = hp.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
alert(inputLine);
in.close();
}
}
 
G

Gene Wirchenko

I want to open a url and read it into a string.
That string will be passed onto a json parser.
I tried this.. but it's doing nothing...

import java.net;
import java.io.*;

function getURL(url) {
URL hp = new URL(url);
URLConnection yc = hp.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
alert(inputLine);
in.close();
}
}

JavaScript is not Java. Try posting to a Java newsgroup.

Sincerely,

Gene Wirchenko
 
S

SpreadTooThin

     JavaScript is not Java.  Try posting to a Java newsgroup.

Sincerely,

Gene Wirchenko

So then there is now way to load the html pointed at by the URL string
into a string in javascript?
 
Z

Zlatko Äurić

JavaScript is not Java. Try posting to a Java newsgroup.

Alternatively, the OP can try with JavaScript.

Maybe:

function getJSONfromUrl(url) {
if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
} else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET",url,false);
xhr.send();
return xhr.responseText;
}

?
 
S

SpreadTooThin

Alternatively, the OP can try with JavaScript.

Maybe:

function getJSONfromUrl(url) {
   if (window.XMLHttpRequest) {
     xhr=new XMLHttpRequest();
   } else {
     xhr=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xhr.open("GET",url,false);
   xhr.send();
   return xhr.responseText;

}

?

Thanks Zlatko.. cool.
 
S

SpreadTooThin

Alternatively, the OP can try with JavaScript.

Maybe:

function getJSONfromUrl(url) {
   if (window.XMLHttpRequest) {
     xhr=new XMLHttpRequest();
   } else {
     xhr=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xhr.open("GET",url,false);
   xhr.send();
   return xhr.responseText;

}

?

Ohh but then does this assume that the server side is a microsoft
windows web server or that the client is?
Perhaps I do need a java applet?
 
S

SpreadTooThin

Alternatively, the OP can try with JavaScript.

Maybe:

function getJSONfromUrl(url) {
   if (window.XMLHttpRequest) {
     xhr=new XMLHttpRequest();
   } else {
     xhr=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xhr.open("GET",url,false);
   xhr.send();
   return xhr.responseText;

}

?

Does the url have to be local? This is almost working... what if the
URL was something like 'www.google.com/?search=foo"
Did I read something about proxy-ing the request?
 
S

Scott Sauyet

SpreadTooThin said:
Does the url have to be local?  This is almost working... what if the
URL was something like 'www.google.com/?search=foo"
Did I read something about proxy-ing the request?

Yes, browsers are generally subject to restrictions allowing them to
request additional documents only from the domain of the requesting
document. If you want to read something else, you will usually need
to proxy it on your server, using whatever server-side techniques you
have available.

-- Scott
 
T

Thomas 'PointedEars' Lahn

Zlatko said:
function getJSONfromUrl(url) {
if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
} else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET",url,false);
xhr.send();
return xhr.responseText;
}

?

The flaws of this code are many:

- Bogus XHR() feature test
- `xhr' not declared a variable
- Missing XHR() exception handling
- Missing ActiveXObject feature test
- Missing XMLHTTP selection
- Missing ActiveXObject() exception handling
- Missing non-XHR()-non-MSXML branch
- Unnecessary asynchronous request blocking the client window/tab
- Missing send() argument
- Missing status check
- Insufficient code style

So it is neither safe nor compatible or user-friendly, but it might suffice
for a first dry run. `url' cannot be any URI though, as the Same Origin
Policy would prevent cross-protocol/domain/port access unless the owner of
the other site explicitly allowed it.

<https://developer.mozilla.org/En/HTTP_access_control>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Zlatko said:
function getJSONfromUrl(url) {
if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
} else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET",url,false);
xhr.send();
return xhr.responseText;
}

?

The flaws of this code are many:

- Bogus XHR() feature test
- `xhr' not declared a variable
- Missing XHR() exception handling
- Missing ActiveXObject feature test
- Missing XMLHTTP selection
- Missing ActiveXObject() exception handling
- Missing non-XHR()-non-MSXML branch
- Unnecessary synchronous request blocking the client window/tab
- Missing send() argument
- Missing status check
- Insufficient code style

So it is neither safe nor compatible or user-friendly, but it might suffice
for a first dry run. `url' cannot be any URI though, as the Same Origin
Policy would prevent cross-protocol/domain/port access unless the owner of
the other site explicitly allowed it.

<https://developer.mozilla.org/En/HTTP_access_control>


PointedEars
 
Z

Zlatko Äurić

The flaws of this code are many:

- Bogus XHR() feature test
- Missing XMLHTTP selection
- Missing non-XHR()-non-MSXML branch
- Unnecessary synchronous request blocking the client window/tab
- Missing status check
- Insufficient code style
PointedEars

Thanks, pointy :)

I am not a wizard at JavaScript, I consider myself a little above the
beginner. And this thing up there, it was written off the top of my
head. In my day work use YUI and it's connection management, so I never
got the hang of doing all the stuff manually. But like I said, it's a
starter that neds a lot of work. To merely work, at that.

Now that we're at it, can you help me grow and explain the things I left
quoted? I understand some of it, but I'd like to hear more.
 
Z

Zlatko Äurić

That form of address is reserved for closer acquaintances.

Ok, thanks for noticing. But in that case, you should probably change
the signature to Mr. Lahn, Thomas, Somebody out there, or however you
would like to be addressed. I think it would save you at least some of
these smart answers.
 
A

Arno Welzel

Zlatko Äurić, 2011-11-24 00:29:
Alternatively, the OP can try with JavaScript.

Maybe:

function getJSONfromUrl(url) {
if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
} else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET",url,false);
xhr.send();
return xhr.responseText;
}


A more complete approach:


function getJSONfromUrl(url)
{
var request=null;
var response="";

try
{
request = new XMLHttpRequest();
}
catch(ms)
{
try
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(nonms)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
request = null;
}
}
}

if(request != null)
{
request.open("GET", url, false);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
request.send(null);
response = request.responseText;
}

return response;
}


But you have to consider that this is only possible if either the target
url is within the same domain as the script (same origin policy) OR if
the target server provides the appropriate CORS headers and the browser
supports CORS at all (see <http://www.w3.org/TR/cors/>).

If you need to access URLs outside your own domain you have to implement
a proxy on your own server which forwards the requests to the target and
returns the results to the script.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top