web services - Call external json webservice from asp.net C# -


i need make call json webservice c# asp.net. service returns json object , json data webservice wants this:

"data" : "my data" 

this i've come can't understand how add data request , send , parse json data back.

string data = "test"; uri address = new uri("http://localhost/service.svc/json"); httpwebrequest request = (httpwebrequest)webrequest.create(address); request.method = "post"; request.contenttype = "application/json; charset=utf-8"; string postdata = "{\"data\":\"" + data + "\"}"; 

how can add json data request , parse response?

use javascriptserializer, deserialize/parse data. can data using:

// corrected webrequest httpwebrequest webrequest request = webrequest.create("http://localhost/service.svc/json");  request.method="post"; request.contenttype = "application/json; charset=utf-8"; string postdata = "{\"data\":\"" + data + "\"}"; //encode data                                                 //using javascript serializer  //get reference request-stream, , write postdata using(stream s = request.getrequeststream()) {     using(streamwriter sw = new streamwriter(s))         sw.write(postdata); }  //get response-stream, , use streamreader read content using(stream s = request.getresponse().getresponsestream()) {     using(streamreader sr = new streamreader(s))     {         var jsondata = sr.readtoend();         //decode jsondata javascript serializer     } } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -