public static T DeserializeResponse <T>(TempResponse tempResponse) { T returnObject = default(T); //var jsSerializer= new JavaScriptSerializer (); //optional - Display the Json Response DisplayJsonResponse(tempResponse.JsonResponse); if (null != tempResponse.JsonResponse) { // parse JSON data into C# obj //returnObject = jsSerializer.Deserialize<T>(tempResponse.JsonResponse); returnObject = JsonConvert.DeserializeObject <T>(tempResponse.JsonResponse); } return(returnObject); }
public TempResponse ProcessTransaction(string methodUrl, string token, string requestData) { // Header details are available at Authentication header page. string Baseurl = ApiEndPointConfiguration.BaseUrl; //Production. // variables for request stream and Respone reader Stream dataStream = null; StreamReader reader = null; WebResponse response = null; TempResponse objTempResponse = new TempResponse(); try { //Set the request header //Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create(Baseurl + methodUrl); // Set the Method property of the request to POST. request.Method = "POST"; //to set HTTP version of the current request, use the Version10 and Version11 fields of the HttpVersion class. ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version11; // optional - to set the Accept property, cast the WebRequest object into HttpWebRequest class //((HttpWebRequest)request).Accept = "*/*"; //Set the ContentType property of the WebRequest. request.ContentType = "application/json"; //set the Authorization token ((HttpWebRequest)request).Headers[HttpRequestHeader.Authorization] = token; byte[] byteArray = Encoding.UTF8.GetBytes(requestData); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // To Get the response. response = request.GetResponse(); // Assuming Response status is OK otherwise catch{} will be excuted // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Assign/store Transaction Json Response to TempResposne Object objTempResponse.JsonResponse = responseFromServer; return(objTempResponse); } catch (WebException e) { // This exception will be raised if the server didn't return 200 - OK within response. // Retrieve more information about the error and API resoponse if (e.Response != null) { //to retrieve the actual JSON response when any error occurs. using (var responseStream = e.Response.GetResponseStream()) { if (responseStream != null) { string temp = (new StreamReader(responseStream)).ReadToEnd(); objTempResponse.JsonResponse = temp; } } //Retrive http Error HttpWebResponse err = (HttpWebResponse)e.Response; if (err != null) { objTempResponse.ErrorMessage = ((int)err.StatusCode) + " " + err.StatusDescription; } } //Do your own error logging in this case } finally { // Clean up the streams. if (null != reader) { reader.Close(); } if (null != dataStream) { dataStream.Close(); } if (null != response) { response.Close(); } } //Do your code here return(objTempResponse); }
public static void AssignError(TempResponse tempResponse, PayTraceBasicResponse basicResponse) { basicResponse.HttpErrorMessage = tempResponse.ErrorMessage; }