private static void GetAsync() { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/4") .Method("GET") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(q => q .Add("fields", "name,first_name,last_name") .Add("format", "json")) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); request.ExecuteAsync(ar => { var response = ar.Response; // seek the save stream to beginning. response.SaveStream.Seek(0, SeekOrigin.Begin); // Print the response Console.WriteLine("GetAsync: "); Console.WriteLine(FluentHttpRequest.ToString(response.SaveStream)); }, null); }
private static void Delete(string postId) { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath(postId) .Method("DELETE") .QueryStrings(qs => qs.Add("access_token", AccessToken)) .Headers(h => h.Add("User-Agent", "FluentHttp")) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); // Execute the request. Call EndRequest immediately so it behaves synchronously. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); // Print the response Console.WriteLine("Delete: "); Console.WriteLine(FluentHttpRequest.ToString(responseSaveStream)); }
private static string PeelOutGistHtmlUrl(FluentHttpAsyncResult response) { response.Response.SaveStream.Seek(0, SeekOrigin.Begin); var gistJson = FluentHttpRequest.ToString(response.Response.SaveStream); dynamic gist = JObject.Parse(gistJson); return((string)gist.html_url); }
public static string UploadPhoto(string path, string filename, string contentType, string message) { var parameters = new Dictionary <string, object>(); parameters["message"] = message; parameters["file1"] = new MediaObject { ContentType = contentType, FileName = Path.GetFileName(filename) } .SetValue(File.ReadAllBytes(path)); // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/photos") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(qs => qs.Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => AttachRequestBodyAndUpdateHeader(body.Request, parameters, null)); // Execute the request. Call EndRequest immediately so it behaves synchronously. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); if (ar.Exception != null) { throw ar.Exception; } // Print the response Console.WriteLine("Upload photo: "); Console.WriteLine(responseResult); if (ar.InnerException != null) { throw ar.InnerException; } return((string)json["id"]); }
private static string Post(string message) { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/feed") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(q => q .Add("format", "json") .Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => { var parameters = new Dictionary <string, object>(); parameters["message"] = message; AttachRequestBodyAndUpdateHeader(body.Request, parameters, null); }); // Execute the request. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); // Print the response Console.WriteLine("Post: "); Console.WriteLine(responseResult); return((string)json["id"]); }