Skip to content

Instantly share code, notes, and snippets.

@smetronic
Last active February 13, 2020 09:09
Show Gist options
  • Save smetronic/5346c666622ad5dd3f288579d3687a88 to your computer and use it in GitHub Desktop.
Save smetronic/5346c666622ad5dd3f288579d3687a88 to your computer and use it in GitHub Desktop.
C# REST client using legacy code, HttpWebRequest
JObject jObjectbody = new JObject();
jObjectbody.Add("label", item.PlateNo);
jObjectbody.Add("asset", item.AssetCode);
jObjectbody.Add("rfid", item.RFID);
var baseAddress = $"http://xx.xx.xx.xx/casper/api/FCS/SecondaryTag?key={userModel.Session}";
var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
string parsedContent = jObjectbody.ToString();
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment