skip to Main Content

I’m making a project in c# to get a facebook ID from any facebook URL. I an API that can helps but the code is using this Ajax POST code:

        // get facebook id
    $('#get-facebook-id').submit(function(event) {

        $('#answer').html("<b>Working for you...</b>");

        $.ajax({
            type: 'POST',
            url: 'https://codeofaninja.com/tools/get-facebook-id-answer.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#answer').html(data);
            $('#page_url_tb').val('')
        })
        .fail(function() {
            alert( "Posting failed. Please try again." );
        });

        return false;
    });

I’m trying to make my own code but is giving me an error. My code and my error down bellow.

private string getFbAccountID(string facebookurl)
    {
        var request = (HttpWebRequest)WebRequest.Create("https://codeofaninja.com/tools/get-facebook-id-answer.php");

        var postData = "page_url="+facebookurl;
        var data = Encoding.ASCII.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        return responseString;
    }

Error:

    <br />
<b>Notice</b>:  Undefined index: page_url in <b>/home/100661.cloudwaysapps.com/rpxdhcedbx/public_html/tools/get-facebook-id-answer.php</b> on line <b>3</b><br />
<br />
<b>Notice</b>:  Undefined offset: 1 in <b>/home/100661.cloudwaysapps.com/rpxdhcedbx/public_html/tools/get-facebook-id-answer.php</b> on line <b>19</b><br />
<p>There was an error. Please try a different input.</p>

This is always giving me this error. I hope someone can helps me

2

Answers


  1. I take it you’re working on this exercise. 🙂

    But to answer your question, you’re not URL encoding facebookurl.

    If you use one of the example Facebook URLs on their page, and watch the network traffic using the developer tools in the browser (F12), you will see this as the form data being sent: page_url=https%3A%2F%2Fwww.facebook.com%2Fpages%2FAndrew-Garfield%2F166865773328160

    jQuery’s serialize() function does that for you.

    In C#, you can use WebUtility.UrlEncode:

    var postData = "page_url=" + WebUtility.UrlEncode(facebookurl);
    
    Login or Signup to reply.
  2. Here you have an alternative using System.Net.WebClient, it will make your code leaner:

    private string getFbAccountID(string facebookurl)
    {
        using (var webClient = new System.Net.WebClient())
        {
            var formValues = new System.Collections.Specialized.NameValueCollection
            {
                { "page_url", facebookurl }
            };
    
            var responseBytes = webClient.UploadValues(
                "https://codeofaninja.com/tools/get-facebook-id-answer.php",
                formValues
                );
    
            return System.Text.Encoding.UTF8.GetString(responseBytes);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search