private string SendJSON() { //Send data in JSON format MyData dataToSend = new MyData() { Id = 1, Desc = "My JSON Desc", StartDate = null }; JavaScriptSerializer ser = new JavaScriptSerializer(); string dataJson = ser.Serialize(dataToSend); var uri = new Uri("http://localhost:8080/TestService.svc/REST/PostData?data=" + dataJson); var request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = "text/json"; request.Method = WebRequestMethods.Http.Post; //In the config file it shouldn't have TransferMode = Streamed using (var response = request.GetResponse()) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); } }
private string SendXML() { //Send data in JSON format MyData dataToSend = new MyData() { Id = 2, Desc = "My XML Desc", StartDate = null }; XmlSerializer ser = new XmlSerializer(dataToSend.GetType()); StringWriter Writer = new StringWriter(); ser.Serialize(Writer, dataToSend); string dataXML = Writer.ToString(); Writer.Close(); var uri = new Uri("http://localhost:8080/TestService.svc/REST/PostDataWithFormat?data=" + dataXML + "&format=xml"); var request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = "text/xml"; request.Method = WebRequestMethods.Http.Post; //In the config file it shouldn't have TransferMode = Streamed using (var response = request.GetResponse()) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); } }