Connecting to reddit

Has anyone tried to connect to reddit via the Ethernet Shield? I’ve been having no luck.
Here’s my code:

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0x57 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(198,41,209,140);  // numeric IP for Google (no DNS)
char server[] = "api.reddit.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /api/multi/all");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

THis is what the Arduino returns:

connecting...
connected
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>Direct IP access not allowed | 11f11 | CloudFlare</title></title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
<style type="text/css">body{margin:0;padding:0}</style>
<!--[if lte IE 9]><script type="text/javascript" src="/cdn-cgi/scripts/jquery.min.js"></script><![endif]-->
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script><!--<![endif]-->
<script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script>

</head>
<body>
  <div id="cf-wrapper">
    <div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
    <div id="cf-error-details" class="cf-error-details-wrapper">
      <div class="cf-wrapper cf-header cf-error-overview">
        <h1>
          <span class="cf-error-type" data-translate="error">Error</span>
          <span class="cf-error-code">1003</span>
          <small class="heading-ray-id">Ray ID: 1a2a91d2085a040c</small>
        </h1>
        <h2 class="cf-subheadline" data-translate="error_desc">Direct IP access not allowed</h2>
      </div><!-- /.header -->

      <section></section><!-- spacer -->

      <div class="cf-section cf-wrapper">
        <div class="cf-columns two">
          <div class="cf-column">
            <h2 data-translate="what_happened">What happened?</h2>
            <p>You've requested an IP address that is part of the <a data-orig-proto="https" data-orig-ref="www.cloudflare.com" target="_blank">CloudFlare</a> network.</p>
          </div>

          
          <div class="cf-column">
            <h2 data-translate="what_can_i_do">What can I do?</h2>
            <p>If you are interested in learning more about CloudFlare, please <a data-orig-proto="https" data-orig-ref="www.cloudflare.com" target="_blank">visit our website</a>.</p>
          </div>
          
        </div>
      </div><!-- /.section -->

      <div class="cf-error-footer cf-wrapper">
  <p>
    <span class="cf-footer-item">CloudFlare Ray ID: <strong>1a2a91d2085a040c</strong></span>
    <span class="cf-footer-separator">&bull;</span>
    <span class="cf-footer-item"><span data-translate="your_ip">Your IP</span>: 70.27.192.105</span>
    <span class="cf-footer-separator">&bull;</span>
    <span class="cf-footer-item"><span data-translate="performance_security_by">Performance &amp; security by</span> <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing" id="brand_link" target="_blank">CloudFlare</a></span>
    
  </p>
</div><!-- /.error-footer -->


    </div><!-- /#cf-error-details -->
  </div><!-- /#cf-wrapper -->

  <script type="text/javascript">
  window._cf_translation = {};
  
  
</script>

</body>
</html>

disconnecting.

Anybody know why CloudFlare isn’t letting me connect?
Thanks

I was struggling with this for awhile but finally figured it out. I assume we're both trying to build some IoT devices that connect to reddit.

Basically, Reddit's hosting company, CloudFare, does not allow direct IP access (something about anycast or whatever). You have to convince the server that you're trying to reach www.reddit.com/xxxx or api.reddit.com/xxxx rather than 198.41.209.141/xxxx

The trick is in how your HTTP headers are set up. In your code, you just connect to the server and

client.println("GET /api/multi/all");

Now, if you were to curl reddit, that's not what your computer is using:

curl --verbose http://www.reddit.com/test/
* About to connect() to www.reddit.com port 80 (#0)
*   Trying 198.41.209.141...
* Connected to www.reddit.com (198.41.209.141) port 80 (#0)
> GET /test/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.reddit.com
> Accept: */*

You'll want to use HTTP headers like this...

client.println("GET /api/multi/all HTTP/1.1\r\nHost: www.reddit.com\r\nAccept: */*");

..Or end the url with .json and accept application/json if that's what you're looking for