Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json call with C# [duplicate]

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.

like image 336
Vaccano Avatar asked Feb 13 '11 06:02

Vaccano


People also ask

Can I use JSON in C?

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.

What is JSON in C?

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.

What is JSON-c#?

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.

How do I post JSON data to API using C #?

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.


2 Answers

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.

like image 133
Shiv Kumar Avatar answered Oct 12 '22 07:10

Shiv Kumar


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; } 
like image 35
jaysonragasa Avatar answered Oct 12 '22 05:10

jaysonragasa