0

I am rewriting code from http://blog.blackballsoftware.com/2010/11/03/making-a-facebook-wall-post-using-the-new-graph-api-and-c/ to create a class to post to Facebook. The code works as long as I do not URLEncode the post data. For example: If the post data is "message=Test,please ignore" then it works. If I URLEncode the same data into "message%3dTest%2cplease+ignore" then I get the error {"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}.

Should the Post data be URLEncoded? I think it should because if I post a message like this, "Test&Message", then only the word Test appears.

Relevant code is below. If postParams = HttpUtility.UrlEncode(postParams); is commented out, then the code works. If not, Facebook returns the error that the message is missing.

        postParams = HttpUtility.UrlEncode(postParams);
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postParams);
        webRequest.ContentLength = bytes.Length;

        System.IO.Stream os = webRequest.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
        os.Close();

        try
        {
            var webResponse = webRequest.GetResponse();
        }

        catch (WebException ex)
        {
            StreamReader errorStream = null;

            errorStream = new StreamReader(ex.Response.GetResponseStream());
            error = errorStream.ReadToEnd() + postParams;

         }
Richard Blevins
  • 115
  • 1
  • 9

1 Answers1

0

The answer can be found on Stackoverflow at C# Escape Plus Sign (+) in POST using HttpWebRequest. Use Uri.EscapeDataString and not URLEncode. Encode the parameter value only and not the equals sign after the parameter name. Example: message=Test%2Cplease%26%20ignore works but message%3dTest%2Cplease%26%20ignore does not work because the equals after the parameter name is encoded as %3d.

Community
  • 1
  • 1
Richard Blevins
  • 115
  • 1
  • 9