Search code examples
facebookwebrequest

How can I fetch full name and picture from any user's Facebook page?


I'd like to get access to the public information about any user on Facebook. Basically, that means only user pictures and full names, that's all. It's the information you get when you open a page without being logged in, like this:

http://www.facebook.com/jurgenappelo

However, when I try to do this from code, Facebook returns this message:

"You are using an incompatible web browser."

I'm trying to mimic a Firefox browser, but that doesn't seem to work. Am I doing something wrong? Or is Facebook using other techniques to block this?

        var requestString = "http://www.facebook.com/jurgenappelo";
        var request = (HttpWebRequest)WebRequest.Create(requestString);
        request.Headers.Add("HTTP_USER_AGENT", "Gecko/20050511 Firefox/1.0.4"); 
        try
        {
            HttpWebResponse response =(HttpWebResponse)request.GetResponse();
            if (response != null)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream stream = response.GetResponseStream();
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        var html = reader.ReadToEnd();
                    }
                }
            response.Close();
            }
        }
        catch { }

Solution

  • According to this:

    Setting HTTP headers in .NET: This header must be modified using the appropriate property

    you should try changing the:

    request.Headers.Add("HTTP_USER_AGENT", "Gecko/20050511 Firefox/1.0.4"); 
    

    to:

    request.UserAgent = "Gecko/20050511 Firefox/1.0.4";