Thursday, June 18, 2015

Sending Rest API Request from C#

public Object FTRJMSRequest(string RequestType, string Type, string SubType, string SubSubType, string QRCType, string FaultFound, string ResolutionType, string Mobile)
 {
 const string URL = "http://00.00.000.00:8080/knowmaxservice/webresources/webservice/SendRequest/";

 string DATA = "{'RequestType':'" + RequestType + "','Type':'" + Type + "','SubType':'" + SubType + "','SubSubType':'" + SubSubType + "','QRCType':'" + QRCType + "','FaultCode':'" + FaultFound + "','ResolutionCode':'" + ResolutionType + "','Mobile':'" + Mobile + "','Notes':'Testing in Progress....','UserName':'Service_Setup'}";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
 request.Method = "POST";
 request.ContentType = "application/json";
 request.ContentLength = DATA.Length;
 using (Stream webStream = request.GetRequestStream())
 using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII)) {
 requestWriter.Write(DATA);
 }
 try {
 WebResponse webResponse = request.GetResponse();
 using (Stream webStream = webResponse.GetResponseStream())
 {
 if (webStream != null) {
 using (StreamReader responseReader = new StreamReader(webStream))
 {
           string response = responseReader.ReadToEnd();
                    objReturn = response.ToString();
 //Console.Out.WriteLine(response.ToString());
 }
 }
}
}
 catch (Exception e)
 {
 //Console.Out.WriteLine("-----------------");
 //Console.Out.WriteLine(e.Message);
 }
 return objReturn;
}

No comments:

Post a Comment