mtanti95 mtanti95 - 4 months ago 8
Java Question

Error 403 when trying to read from Public Stream

I'm trying to retrieve the data of this stream from Meetup - http://stream.meetup.com/2/open_events

but I keep getting error 403 using the code below

URL meetupStream = new URL("http://stream.meetup.com/2/open_events");
BufferedReader in = new BufferedReader(new InputStreamReader(meetupStream.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();


The stream does not need any authentication and can be seen normally in a browser

Any ideas what could be causing such an error?

Answer

Some webservers complain if your request looks too bleak, may be because they think it was generated by machines? Typically they can tell by the headers you sent. This should already do it:

    URLConnection connection = new URL("http://stream.meetup.com/2/open_events").
                                 openConnection();
    connection.setRequestProperty("User-Agent", "Lynx");
    connection.connect();
    BufferedReader in = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();
Comments