Assuming the service returns JSON , we must follow the steps:
Create the RestfUL request URI.
Publish URI and get the answer of HttpWebResponse
.
Converts ResponseStreem to serialized object of function DataContractJsonSerialized
.
Get the results / particular elements of the serialized object.
That's the code in C # something generic
public static object MakeRequest(string requestUrl, object JSONRequest, string JSONmethod, string JSONContentType, Type JSONResponseType) {
try {
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
//WebRequest WR = WebRequest.Create(requestUrl);
string sb = JsonConvert.SerializeObject(JSONRequest);
request.Method = JSONmethod;
// "POST";request.ContentType = JSONContentType; // "application/json";
Byte[] bt = Encoding.UTF8.GetBytes(sb);
Stream st = request.GetRequestStream();
st.Write(bt, 0, bt.Length);
st.Close();
using(HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
if (response.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).", response.StatusCode,
response.StatusDescription));
// DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));// object objResponse = JsonConvert.DeserializeObject();Stream stream1 = response.GetResponseStream();
StreamReader sr = new StreamReader(stream1);
string strsb = sr.ReadToEnd();
object objResponse = JsonConvert.DeserializeObject(strsb, JSONResponseType);
return objResponse;
}
} catch (Exception e) {
Console.WriteLine(e.Message);
return null;
}
}