Read url into a string?

Discussion in 'Javascript' started by SpreadTooThin, Nov 23, 2011.

  1. 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();
    }
    }
     
    SpreadTooThin, Nov 23, 2011
    #1
    1. Advertisements

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

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 23, 2011
    #2
    1. Advertisements

  3. So then there is now way to load the html pointed at by the URL string
    into a string in javascript?
     
    SpreadTooThin, Nov 23, 2011
    #3
  4. 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;
    }

    ?
     
    Zlatko Äurić, Nov 23, 2011
    #4
  5. Thanks Zlatko.. cool.
     
    SpreadTooThin, Nov 23, 2011
    #5
  6. 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?
     
    SpreadTooThin, Nov 23, 2011
    #6
  7. 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?
     
    SpreadTooThin, Nov 24, 2011
    #7
  8. SpreadTooThin

    Scott Sauyet Guest

    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
     
    Scott Sauyet, Nov 24, 2011
    #8
  9. 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
     
    Thomas 'PointedEars' Lahn, Nov 24, 2011
    #9
  10. 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
     
    Thomas 'PointedEars' Lahn, Nov 24, 2011
    #10
  11. 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.
     
    Zlatko Äurić, Nov 24, 2011
    #11
  12. That form of address is reserved for closer acquaintances.
    <http://www.catb.org/~esr/faqs/smart-questions.html>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 24, 2011
    #12
  13. 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.
     
    Zlatko Äurić, Nov 24, 2011
    #13
  14. SpreadTooThin

    Arno Welzel Guest

    Zlatko Äurić, 2011-11-24 00:29:

    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.
     
    Arno Welzel, Nov 25, 2011
    #14
  15. SpreadTooThin

    Evertjan. Guest

    Java != Javascript
     
    Evertjan., Nov 26, 2011
    #15
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.