Skip to content

Instantly share code, notes, and snippets.

@reza-ryte-club
Last active August 29, 2015 14:21
Show Gist options
  • Save reza-ryte-club/19325afa41af52d550ec to your computer and use it in GitHub Desktop.
Save reza-ryte-club/19325afa41af52d550ec to your computer and use it in GitHub Desktop.
Xamarin Implementation of POST request
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace Aduuu
{
class MainClass
{
public static void Main (string[] args)
{
//POST request
WebClient wc = new WebClient();
string baseSiteString = wc.DownloadString("http://damp-sands-2243.herokuapp.com/students");
string csrfToken = Regex.Match(baseSiteString, "<meta name=\"csrf-token\" content=\"(.*?)\" />").Groups[1].Value;
string cookie = wc.ResponseHeaders[HttpResponseHeader.SetCookie];
Console.WriteLine("Token: {0}", csrfToken);
Console.WriteLine("Cookie: {0}", cookie);
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
wc.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
wc.Headers.Add("X-CSRF-Token", csrfToken);
wc.Headers.Add("X-Requested-With", "XMLHttpRequest");
string dataString = @"{""name"":""Meg"",""regi_number"":3800}";
byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
byte[] responseBytes = wc.UploadData(new Uri("https://damp-sands-2243.herokuapp.com/students/"), "POST", dataBytes);
string responseString = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(responseString);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment