Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. private static T SendRequest<T>(string methodName, string requestData)
  2.         {
  3.             WebClient client = new WebClient();
  4.             try
  5.             {
  6.                 string serviceUrl = System.Configuration.ConfigurationManager.AppSettings["WS.URL"];
  7.                 dynamic logRequest = JObject.Parse(requestData);
  8.                 logRequest.passwd = "NO VIEW in logs";
  9.  
  10.                 log.Trace(string.Format("{0}{1}\n{2}", serviceUrl, methodName, logRequest.ToString()));
  11.                
  12.                 client.Headers["Content-type"] = "application/json";
  13.                 MemoryStream stream = new MemoryStream();
  14.  
  15.                 byte[] data;// = client.UploadData(string.Format("{0}{1}", serviceUrl, methodName), "POST", Encoding.Default.GetBytes(requestData));
  16.                 var task = Task.Run(() => client.UploadData(string.Format("{0}{1}", serviceUrl, methodName), "POST", Encoding.UTF8.GetBytes(requestData)));
  17.                 if ((task.Wait(TimeSpan.FromSeconds(15)) && (methodName == "GetTenantsToPrint" || methodName == "RegisterVips")) || methodName != "GetTenantsToPrint")
  18.                 {
  19.                     data = task.Result;
  20.                 }
  21.                 else
  22.                     throw new Exception("Timed out");
  23.                 stream = new MemoryStream(data);
  24.                 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
  25.                 T response = (T)serializer.ReadObject(stream);
  26.                 //(T)serializer.ReadObject()
  27.                 log.Trace(JsonConvert.SerializeObject(response));
  28.                 return response;
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 string method = string.Format("{0}.{1}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, System.Reflection.MethodBase.GetCurrentMethod().Name);
  33.                 log.Error(ex, method);
  34.                 return default(T);
  35.             }  
  36.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement