Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region JSON API
- public static string ObjectToJson(object o, Type t, Type[] pTypes = null)
- {
- DataContractJsonSerializer serializer = (pTypes != null ? new DataContractJsonSerializer(t, pTypes) : new DataContractJsonSerializer(t));
- MemoryStream mStrm = new MemoryStream();
- serializer.WriteObject(mStrm, o);
- mStrm.Position = 0;
- using (var sr = new StreamReader(mStrm, Encoding.UTF8))
- {
- return sr.ReadToEnd();
- }
- }
- public static object JsonToObject(string json, Type t, Type[] pTypes = null)
- {
- DataContractJsonSerializer serializer = (pTypes != null ? new DataContractJsonSerializer(t, pTypes) : new DataContractJsonSerializer(t));
- using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
- {
- return serializer.ReadObject(stream);
- }
- }
- #endregion // JSON API
- public bool ListOfGames(bool pMyGames, bool pGamesITookPartIn)
- {
- this.GamesListInfo = null;
- this.LastErrorInfo = null;
- try
- {
- string formValues =
- "&playerId=" + this.LogonInfo.userId.ToString() +
- "&createdBy=" + pMyGames.ToString() +
- "&tookPartIn=" + pGamesITookPartIn.ToString() +
- "";
- WebRequest request = createRequest("myGamesList", formValues);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- using (StreamReader sr = new StreamReader(response.GetResponseStream()))
- {
- string responseText = sr.ReadToEnd();
- if (responseText.Contains(""errType":"))
- this.LastErrorInfo = (ErrorInfoContract)JsonToObject(responseText, typeof(ErrorInfoContract));
- else
- this.GamesListInfo = (GamesListContract)JsonToObject(responseText, typeof(GamesListContract), new Type[] { typeof(GameInfoContract) });
- Trace.WriteLineIf(TrcLvl.TraceInfo, TrcLvl.TraceInfo ? string.Format("NewGame.ListOfGames: {0}{1}",
- (this.LogonInfo != null ? this.LogonInfo.ToString() : ""), (this.LastErrorInfo != null ? this.LastErrorInfo.ToString() : "")) : "");
- }
- }
- catch (Exception exc)
- {
- this.LastErrorInfo = new ErrorInfoContract()
- {
- errType = exc.GetType().ToString(),
- message = exc.Message,
- stackTrace = exc.StackTrace,
- timestamp = StrUtils.NskTimestampOf(DateTime.Now)
- };
- }
- return (this.LogonInfo != null && this.LogonInfo.userId > 0 && this.LogonInfo.authKey > 0);
- }
- private WebRequest createRequest(string pAction, string pFormValues)
- {
- WebRequest request = WebRequest.Create(this.ServerUrl + "?action=" + pAction);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = pFormValues.Length;
- byte[] data = Encoding.UTF8.GetBytes(pFormValues);
- using (Stream strm = request.GetRequestStream())
- {
- strm.Write(data, 0, data.Length);
- }
- return request;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement