C# HTTP POST and GET
Dabbling in C# a little bit, and wanted to note some partial code (albeit legacy) for post/get against web. For e.g. json and xml APIs.
string postString = ""; // Log into local PMS. var webcall = (HttpWebRequest)WebRequest.Create(PMS_AUTH_URL); postString += "username=" + PMS_USERNAME; postString += "&password=" + PMS_PASSWORD; var postStringEnc = System.Text.Encoding.UTF8.GetBytes(postString); webcall.Method = "POST"; webcall.ContentType = "application/x-www-form-urlencoded"; webcall.ContentLength = postStringEnc.Length; webcall.Headers.Add("Content-Length","0"); webcall.Headers.Add("X-Plex-Client-Identifier","PulsePlexWatch"); using (var stream = webcall.GetRequestStream()) { stream.Write(postStringEnc, 0, postStringEnc.Length); } var callres = (HttpWebResponse)webcall.GetResponse(); var calltxt = new System.IO.StreamReader(callres.GetResponseStream()).ReadToEnd(); // Check login success. XmlDocument pmsXml = new XmlDocument(); pmsXml.LoadXml(calltxt); XmlNode pmsXmlNode = pmsXml.SelectSingleNode("user/authentication-token"); if (pmsXmlNode == null) return "PMS Login to retrieve token failed."; else // Do something with pmsXmlNode.InnerText.Trim();