Last active
December 18, 2015 10:28
-
-
Save ryanvalentin/5768367 to your computer and use it in GitHub Desktop.
Post a guest comment through the Disqus API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Method to post a new comment | |
/// </summary> | |
public static async Task<string> PostComment(string threadid, string message, string guestemail, string guestname) | |
{ | |
if (!NetworkInterface.GetIsNetworkAvailable()) | |
return "Error: Not connected to the internet"; | |
// Create HTTP client | |
var gziphandler = new CompressedHttpClientHandler(); | |
var httpClient = new HttpClient(gziphandler); | |
httpClient.DefaultRequestHeaders.Referrer = new Uri("http://example.com/", UriKind.Absolute); // Must match one of your API application's trusted domains | |
httpClient.DefaultRequestHeaders.Host = "example.com"; // Must match one of your API application's trusted domains | |
// This is the postdata | |
var postData = new List<KeyValuePair<string, string>>(); | |
postData.Add(new KeyValuePair<string, string>("api_key", ApiResources.key_public)); | |
postData.Add(new KeyValuePair<string, string>("api_secret", ApiResources.key_secret)); | |
postData.Add(new KeyValuePair<string, string>("thread", threadid)); | |
postData.Add(new KeyValuePair<string, string>("message", message)); | |
postData.Add(new KeyValuePair<string, string>("author_email", guestemail)); | |
postData.Add(new KeyValuePair<string, string>("author_name", guestname)); | |
postData.Add(new KeyValuePair<string, string>("strict", "1")); | |
var response = await httpClient.PostAsync(new Uri(ApiResources.endpoint_posts_create, UriKind.Absolute), new FormUrlEncodedContent(postData)); | |
// Return content as a string | |
string content = await response.Content.ReadAsStringAsync(); | |
try | |
{ | |
response.EnsureSuccessStatusCode(); | |
return content; | |
} | |
catch (HttpRequestException) | |
{ | |
try | |
{ | |
JObject resultObject = JObject.Parse(content); | |
// Get code | |
return "Error: " + content; | |
} | |
catch | |
{ | |
return "Error: " + content; | |
} | |
} | |
catch | |
{ | |
return "Error: " + content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment