Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #region JSON API
  2.  
  3. public static string ObjectToJson(object o, Type t, Type[] pTypes = null)
  4. {
  5. DataContractJsonSerializer serializer = (pTypes != null ? new DataContractJsonSerializer(t, pTypes) : new DataContractJsonSerializer(t));
  6. MemoryStream mStrm = new MemoryStream();
  7. serializer.WriteObject(mStrm, o);
  8. mStrm.Position = 0;
  9. using (var sr = new StreamReader(mStrm, Encoding.UTF8))
  10. {
  11. return sr.ReadToEnd();
  12. }
  13. }
  14.  
  15. public static object JsonToObject(string json, Type t, Type[] pTypes = null)
  16. {
  17. DataContractJsonSerializer serializer = (pTypes != null ? new DataContractJsonSerializer(t, pTypes) : new DataContractJsonSerializer(t));
  18. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  19. {
  20. return serializer.ReadObject(stream);
  21. }
  22. }
  23.  
  24. #endregion // JSON API
  25.  
  26. public bool ListOfGames(bool pMyGames, bool pGamesITookPartIn)
  27. {
  28. this.GamesListInfo = null;
  29. this.LastErrorInfo = null;
  30. try
  31. {
  32. string formValues =
  33. "&playerId=" + this.LogonInfo.userId.ToString() +
  34. "&createdBy=" + pMyGames.ToString() +
  35. "&tookPartIn=" + pGamesITookPartIn.ToString() +
  36. "";
  37.  
  38. WebRequest request = createRequest("myGamesList", formValues);
  39. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  40. using (StreamReader sr = new StreamReader(response.GetResponseStream()))
  41. {
  42. string responseText = sr.ReadToEnd();
  43.  
  44. if (responseText.Contains(""errType":"))
  45. this.LastErrorInfo = (ErrorInfoContract)JsonToObject(responseText, typeof(ErrorInfoContract));
  46. else
  47. this.GamesListInfo = (GamesListContract)JsonToObject(responseText, typeof(GamesListContract), new Type[] { typeof(GameInfoContract) });
  48.  
  49. Trace.WriteLineIf(TrcLvl.TraceInfo, TrcLvl.TraceInfo ? string.Format("NewGame.ListOfGames: {0}{1}",
  50. (this.LogonInfo != null ? this.LogonInfo.ToString() : ""), (this.LastErrorInfo != null ? this.LastErrorInfo.ToString() : "")) : "");
  51. }
  52. }
  53. catch (Exception exc)
  54. {
  55. this.LastErrorInfo = new ErrorInfoContract()
  56. {
  57. errType = exc.GetType().ToString(),
  58. message = exc.Message,
  59. stackTrace = exc.StackTrace,
  60. timestamp = StrUtils.NskTimestampOf(DateTime.Now)
  61. };
  62. }
  63. return (this.LogonInfo != null && this.LogonInfo.userId > 0 && this.LogonInfo.authKey > 0);
  64. }
  65.  
  66. private WebRequest createRequest(string pAction, string pFormValues)
  67. {
  68. WebRequest request = WebRequest.Create(this.ServerUrl + "?action=" + pAction);
  69.  
  70. request.Method = "POST";
  71. request.ContentType = "application/x-www-form-urlencoded";
  72. request.ContentLength = pFormValues.Length;
  73.  
  74. byte[] data = Encoding.UTF8.GetBytes(pFormValues);
  75. using (Stream strm = request.GetRequestStream())
  76. {
  77. strm.Write(data, 0, data.Length);
  78. }
  79. return request;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement