I am trying to make a json call using C#. I made a stab at creating a call, but it did not work:
public bool SendAnSMSMessage(string message) { HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://api.pennysms.com/jsonrpc"); request.Method = "POST"; request.ContentType = "application/json"; string json = "{ \"method\": \"send\", "+ " \"params\": [ "+ " \"IPutAGuidHere\", "+ " \"[email protected]\", "+ " \"MyTenDigitNumberWasHere\", "+ " \""+message+"\" " + " ] "+ "}"; StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(json); writer.Close(); return true; }
Any advice on how to make this work would be appreciated.
Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. JSON stands for JavaScript Object Notation. The format was specified by Douglas Crockford.
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to RFC 7159.
To post JSON to a REST API endpoint using C#/. NET, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the C#/. NET POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.
In your code you don't get the HttpResponse, so you won't see what the server side sends you back.
you need to get the Response similar to the way you get (make) the Request. So
public static bool SendAnSMSMessage(string message) { var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.pennysms.com/jsonrpc"); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{ \"method\": \"send\", " + " \"params\": [ " + " \"IPutAGuidHere\", " + " \"[email protected]\", " + " \"MyTenDigitNumberWasHere\", " + " \"" + message + "\" " + " ] " + "}"; streamWriter.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var responseText = streamReader.ReadToEnd(); //Now you have your response. //or false depending on information in the response return true; } }
I also notice in the pennysms documentation that they expect a content type of "text/json" and not "application/json". That may not make a difference, but it's worth trying in case it doesn't work.
just continuing what @Mulki made with his code
public string WebRequestinJson(string url, string postData) { string ret = string.Empty; StreamWriter requestWriter; var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; if (webRequest != null) { webRequest.Method = "POST"; webRequest.ServicePoint.Expect100Continue = false; webRequest.Timeout = 20000; webRequest.ContentType = "application/json"; //POST the data. using (requestWriter = new StreamWriter(webRequest.GetRequestStream())) { requestWriter.Write(postData); } } HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse(); Stream resStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(resStream); ret = reader.ReadToEnd(); return ret; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With