Created
March 1, 2017 10:29
-
-
Save birksy89/95daa17fa4ce67313d7b43ca4ceb1b04 to your computer and use it in GitHub Desktop.
Post JSON Data From Serverside
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
using Newtonsoft.Json; | |
using System.Net; | |
using System.IO; | |
public void postJSON() | |
{ | |
postData pd = new postData(); | |
pd.Name = "Steve"; | |
pd.Age = 54; | |
pd.Sex = "M"; | |
string jsonObj = JsonConvert.SerializeObject(pd); | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://rest.learncode.academy/api/birksy89/a"); | |
httpWebRequest.ContentType = "application/json"; | |
httpWebRequest.Method = "POST"; | |
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) | |
{ | |
streamWriter.Write(jsonObj); | |
} | |
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
var result = streamReader.ReadToEnd(); | |
} | |
} | |
public class postData | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public string Sex { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment