Search code examples
c#httphttpwebrequestcloudflarehttp-status-code-503

Httpresponse Error 503


I'm trying to download a web page data from a website that is hosted by CloudFlare. It uses HTTPS and gets ID for the connection before getting into the page.

I'm trying to get the id in WebRequest and WebResponse but I'm getting the following error:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote server returned an error: (503) Server Unavailable.

I tried to make the request from Fiddler and here is the response:

HTTP/1.1 503 Service Temporarily Unavailable
Date: Tue, 12 May 2015 13:38:17 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d57a7d982035dad7ebafe63444d125e451431437897; expires=Wed, 11-May-16 13:38:17 GMT; path=/; domain=.hornystress.me; HttpOnly
X-Frame-Options: SAMEORIGIN
Refresh: 8;URL=/cdn-cgi/l/chk_jschl?pass=1431437901.815-Ym1g5qTodK
Cache-Control: no-cache
Server: cloudflare-nginx
CF-RAY: 1e5685ed559606ee-LHR

Here is my code :

public static string GetCookie(string link)
{
    WebRequest request = WebRequest.Create("https://hornystress.me");
    request.Proxy = WebProxy.GetDefaultProxy();
    request.Timeout *= 100;
    WebResponse response = request.GetResponse();
    return response.Headers.Get("Set-Cookie");
}

Solution

  • the problem was the the website normal response is Error code 503 with a cookie that i want... so the compiler throws a WebException error;wich i should catch...

        public static string GetCookie()
        {
            WebRequest request = WebRequest.Create("https://hornystress.me");
            request.Proxy = WebProxy.GetDefaultProxy();
            request.Timeout *= 100;
            string cookie;
             WebResponse response;
            try
            {
              response  = request.GetResponse();
              cookie = response.Headers.Get("Set-Cookie");
            }
            catch (WebException we)
            {
               cookie=we.Response.Headers.Get("Set-Cookie");
            }
            return cookie;
        }