call external json webservice from asp.net c#

Jacob
Jacob
34 Points
17 Posts

Hi,

I want to call a third party JSON web service. What is the best way to call JSON web service.

thanks...

Views: 8275
Total Answered: 1
Total Marked As Answer: 1
Posted On: 30-Aug-2015 01:03

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         

Hi Jacob,

Use WebRequest object to call third party json service:

Include following package:

using System.IO;
using System.Net;
using System.Web.Script.Serialization;

 Example:

WebRequest request = WebRequest.Create("Json service path");
 
string JsonString;
 
try
{
 
WebResponse response = request.GetResponse(); 
using (Stream responseStream = response.GetResponseStream())
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
JsonString = reader.ReadToEnd();
}
 
JavaScriptSerializer ScriptSerializer = new JavaScriptSerializer(); 
dynamic JsonObject = ScriptSerializer.Deserialize<dynamic>(JsonString);
//use JsonObject to retrieve json data
}catch (WebException ex)
WebResponse errorResponse = ex.Response; 
using (Stream responseStream = errorResponse.GetResponseStream())
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); 
String errorText = reader.ReadToEnd(); 
// log errorText
}
}

 

Posted On: 05-Sep-2015 02:29
 Log In to Chat