Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8574 > unrolled thread

Read url into a string?

Started bySpreadTooThin <bjobrien62@gmail.com>
First post2011-11-23 14:09 -0800
Last post2011-11-26 13:44 -0400
Articles 14 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 14:09 -0800
    Re: Read url into a string? Gene Wirchenko <genew@ocis.net> - 2011-11-23 15:00 -0800
      Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:28 -0800
      Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 00:29 +0100
        Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:34 -0800
        Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:37 -0800
        Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 16:35 -0800
          Re: Read url into a string? Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-23 18:02 -0800
        Re: Read url into a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-24 21:27 +0100
          Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 22:13 +0100
            Re: Read url into a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-24 22:31 +0100
              Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 23:08 +0100
        Re: Read url into a string? Arno Welzel <usenet@arnowelzel.de> - 2011-11-25 15:59 +0100
    Re: Read url into a string? "Evertjan." <test@abc.invalid> - 2011-11-26 13:44 -0400

#8574 — Read url into a string?

FromSpreadTooThin <bjobrien62@gmail.com>
Date2011-11-23 14:09 -0800
SubjectRead url into a string?
Message-ID<a0faab1d-920c-4d58-bb9f-b382b0b4ce38@w3g2000vbw.googlegroups.com>
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();
   }
}

[toc] | [next] | [standalone]


#8575

FromGene Wirchenko <genew@ocis.net>
Date2011-11-23 15:00 -0800
Message-ID<pmuqc7t8p6ees0qo6sh484mc2o7jvv4afj@4ax.com>
In reply to#8574
On Wed, 23 Nov 2011 14:09:10 -0800 (PST), SpreadTooThin
<bjobrien62@gmail.com> wrote:

>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

[toc] | [prev] | [next] | [standalone]


#8576

FromSpreadTooThin <bjobrien62@gmail.com>
Date2011-11-23 15:28 -0800
Message-ID<ae3288f7-7abd-4e67-bcd1-ff6e3d5c6f0a@c18g2000yqj.googlegroups.com>
In reply to#8575
On Nov 23, 4:00 pm, Gene Wirchenko <ge...@ocis.net> wrote:
> On Wed, 23 Nov 2011 14:09:10 -0800 (PST), SpreadTooThin
>
>
>
>
>
>
>
>
>
> <bjobrie...@gmail.com> wrote:
> >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

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

[toc] | [prev] | [next] | [standalone]


#8577

FromZlatko Đurić <zladuric@gmail.com>
Date2011-11-24 00:29 +0100
Message-ID<jajvki$fvb$1@sunce.iskon.hr>
In reply to#8575
On 11/24/2011 12:00 AM, Gene Wirchenko wrote:

>       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;
}

?


-- 
Zlatko

[toc] | [prev] | [next] | [standalone]


#8578

FromSpreadTooThin <bjobrien62@gmail.com>
Date2011-11-23 15:34 -0800
Message-ID<af682d71-bbab-41c0-8b91-3afebf37ed7e@y42g2000yqh.googlegroups.com>
In reply to#8577
On Nov 23, 4:29 pm, Zlatko Đurić <zladu...@gmail.com> wrote:
> On 11/24/2011 12:00 AM, Gene Wirchenko wrote:
>
> >       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;
>
> }
>
> ?
>
> --
> Zlatko

Thanks Zlatko.. cool.

[toc] | [prev] | [next] | [standalone]


#8581

FromSpreadTooThin <bjobrien62@gmail.com>
Date2011-11-23 15:37 -0800
Message-ID<a8385f2e-0efb-43fa-8dd8-c7e0ef7447c7@n14g2000vbn.googlegroups.com>
In reply to#8577
On Nov 23, 4:29 pm, Zlatko Đurić <zladu...@gmail.com> wrote:
> On 11/24/2011 12:00 AM, Gene Wirchenko wrote:
>
> >       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;
>
> }
>
> ?
>
> --
> Zlatko

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?

[toc] | [prev] | [next] | [standalone]


#8583

FromSpreadTooThin <bjobrien62@gmail.com>
Date2011-11-23 16:35 -0800
Message-ID<ea35434a-767e-4f1c-bfce-4cbd985fa46c@h42g2000yqd.googlegroups.com>
In reply to#8577
On Nov 23, 4:29 pm, Zlatko Đurić <zladu...@gmail.com> wrote:
> On 11/24/2011 12:00 AM, Gene Wirchenko wrote:
>
> >       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;
>
> }
>
> ?
>
> --
> Zlatko

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?

[toc] | [prev] | [next] | [standalone]


#8587

FromScott Sauyet <scott.sauyet@gmail.com>
Date2011-11-23 18:02 -0800
Message-ID<f6f44bfe-bd07-480a-ab90-50d9260204cc@s6g2000vbc.googlegroups.com>
In reply to#8583
SpreadTooThin wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#8605

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-11-24 21:27 +0100
Message-ID<2567734.rclGbaif12@PointedEars.de>
In reply to#8577
Zlatko Đurić wrote:

> 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
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#8608

FromZlatko Đurić <zladuric@gmail.com>
Date2011-11-24 22:13 +0100
Message-ID<jamc1s$ab7$1@sunce.iskon.hr>
In reply to#8605
On 11/24/2011 09:27 PM, Thomas 'PointedEars' Lahn wrote:
>
> 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.

-- 
Zlatko

[toc] | [prev] | [next] | [standalone]


#8609

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-11-24 22:31 +0100
Message-ID<1787296.I8a1CPdyJA@PointedEars.de>
In reply to#8608
Zlatko Đurić wrote:

> Thomas 'PointedEars' Lahn wrote:
>> 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 :)

That form of address is reserved for closer acquaintances.

> […]
> Now that we're at it, can you help me grow and explain the things I left
> quoted?

Yes.

> I understand some of it, but I'd like to hear more.

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

[toc] | [prev] | [next] | [standalone]


#8610

FromZlatko Đurić <zladuric@gmail.com>
Date2011-11-24 23:08 +0100
Message-ID<jamf9f$dn8$1@sunce.iskon.hr>
In reply to#8609
On 11/24/2011 10:31 PM, Thomas 'PointedEars' Lahn wrote:
> 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.

-- 
Zlatko

[toc] | [prev] | [next] | [standalone]


#8628

FromArno Welzel <usenet@arnowelzel.de>
Date2011-11-25 15:59 +0100
Message-ID<4ECFAD36.7070800@arnowelzel.de>
In reply to#8577
Zlatko Đurić, 2011-11-24 00:29:

> On 11/24/2011 12:00 AM, Gene Wirchenko wrote:
> 
>>       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;
> }


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
http://arnowelzel.de
http://de-rec-fahrrad.de

[toc] | [prev] | [next] | [standalone]


#8653

From"Evertjan." <test@abc.invalid>
Date2011-11-26 13:44 -0400
Message-ID<4ed12579$0$19718$e4fe514c@dreader34.news.xs4all.nl>
In reply to#8574
Java != Javascript

On 23-11-2011 18:09, SpreadTooThin wrote:
> 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();
>     }
> }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web