I get an error code 400 “Bad request” when I try to post to a page wall. Here’s the code:
public string Post(Int64 Id)
{
using (DataContext db = new DataContext())
{
var msg = (from t1 in db.Table1
join t2 in db.Table2 on t1.UserId equals t2.UserId
where t1.Id == Id
select new {t1, t2}).FirstOrDefault();
var url = "https://graph.facebook.com/" + msg.t2.Table3.FBPageId + "/feed";
AppendQueryString(ref url, "access_token", msg.t2.Table3.FacebookAuth);
AppendQueryString(ref url, "privacy", "{\"value\": \"EVERYONE\"}");
AppendQueryString(ref url, "message", msg.t1.Message);
var webRequest = WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(url);
webRequest.ContentLength = bytes.Length;
System.IO.Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
try
{
var webResponse = webRequest.GetResponse();
}
catch (Exception ex)
{
return ex.Stacktrace;
}
return "Something random";
}
The code ends at the return in catch (webRequest.GetResponse(); failed).
AppendQueryString is just a method that generates the query string (? or &) and returns the new url.
Am I doing anything wrong?
I had to split up the “base” url and the parameters into two, and put the parameters into
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);and keep the url invar webRequest = WebRequest.Create(url);
Works now