C#

Features ›› Game ›› getGameQuestion ›› Sample Code ››
Parent Previous Next

Get Game Question



1. getGameQuestion.cs



These four packages are required to be used.








Step 1. Create class file to store content of the web response.










2. getGameQuestion.aspx.cs



These 13 packages are required to be used.








Step 2. Store details entered by the user. If user id, password, and OTP exist in session, user need not enter again (refer to common stuff).








Step 3. Create URL which will be post to the server. User inputs need to be stored in the class created, and serialize into JSON format. In this function, only header is needed.








Step 4. Post to the server, and store response. URL is sent using web request function, and a response will be send back from the server. The response need to be deserialized into the ‘getGameQuestion’ class.








Step 5. Obtain error code. If error code is ‘010000’ which means invocation successful, populate attributes of the ‘getGameQuestion’ class into the label. If error code is '010041', it means OTP has expired, and we will use the newOTP() function to prompt the user for the OTP again. Else, we will display the ErrorText and ErrorDetails.


The newOTP() function can be referred to under Common Stuff Section








Overview of getGameQuestion.cs



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Demo

{

   public class getQuestion

   {

       public getGameQuestionContent Content { get; set; }

   }


   public class getGameQuestionContent

   {

       public getGameQuestionServiceResponse ServiceResponse { get; set; }

   }


   public class getGameQuestionServiceResponse

   {

       public QuestionDetails QuestionDetails { get; set; }

       public getGameQuestionServiceRespHeader ServiceRespHeader { get; set; }

   }


   public class QuestionDetails

   {

       public string Choices { get; set; }

       public string Category { get; set; }

       public string Question { get; set; }

       public string QuestionId { get; set; }

   }


   public class getGameQuestionServiceRespHeader

   {

       public string ErrorText { get; set; }

       public object ErrorDetails { get; set; }

       public string GlobalErrorID { get; set; }

   }

}



Download






Overview of getGameQuestion.aspx.cs



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using DotNetOpenAuth.OAuth2;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using System.Collections.Specialized;

using System.Configuration;

using System.Net;

using System.IO;

using System.Windows.Forms;

using System.Data;


namespace Demo

{

   public partial class getGameQuestion : System.Web.UI.Page

   {

       protected void Page_Load(object sender, EventArgs e)

       {

           if (Session["userid"] != null)

           {

               txtUserID.Text = (string)Session["userid"];

               txtPassword.Text = (string)Session["pin"];

           }

       }


       protected void btnSend_Click(object sender, EventArgs e)

       {

           try

           {

               // take user input

               string userID = txtUserID.Text;

               Session["userid"] = userID;

               string pin = txtPassword.Text;

               Session["pin"] = pin;

               string questionID = txtQuestionID.Text;

               string otp;

               if (Session["otp"] == null)

               {

                   otp = "";

               }

               else

               {

                   otp = Session["otp"].ToString();

               }


               // build header

               var header = new HeaderJson();

               header.userID = userID;

               header.PIN = pin;

               header.serviceName = "getGameQuestion";

               header.OTP = otp;

               var headerJson = new HeaderJsonObject();

               headerJson.Header = header;

               string strHeader = JsonConvert.SerializeObject(headerJson);


               // build content

               var content = new ContentJson();

               content.questionID = questionID;

               var contentJson = new ContentJsonObject();

               contentJson.Content = content;

               string strContent = JsonConvert.SerializeObject(contentJson);


               string url = "http://tbankonline.com/SMUtBank_API/Gateway?Header=" + strHeader + "&Content=" + strContent;


               // send web request and store response

               var webRequest = (HttpWebRequest)WebRequest.Create(url);

               webRequest.ContentType = "application/json";

               webRequest.Method = "POST";

               var result = "";

               var httpResponse = (HttpWebResponse)webRequest.GetResponse();

               using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

               {

                   result = streamReader.ReadToEnd();

               }


               // parse result into getQuestion class

               getQuestion gt = new getQuestion();

               gt = JsonConvert.DeserializeObject<getQuestion>(result);


               string ErrorID = gt.Content.ServiceResponse.ServiceRespHeader.GlobalErrorID?.ToString() ?? "-";

               if (ErrorID == "010000")

               {

                   string[] lable = { "a)", "b)", "c)", "d)", "e)" };


                   string c = gt.Content.ServiceResponse.QuestionDetails.Choices.ToString();

                   string[] delimiter = { "&&"};

                   string[] choices = c.Split(delimiter, StringSplitOptions.None);

                   int count = choices.Count();

                   string finalc = null;

                   for (int i = 0; i < count; i++)

                   {

                       finalc = finalc + lable[i] + " " + choices[i] + Environment.NewLine;

                   }


                   txtCategory.Text = gt.Content.ServiceResponse.QuestionDetails.Category.ToString();

                   string questionWithAnswer = gt.Content.ServiceResponse.QuestionDetails.Question.ToString();

                   questionWithAnswer = questionWithAnswer + Environment.NewLine + Environment.NewLine + finalc;

                   txtQuestion.Text = questionWithAnswer;

               }

               else if (ErrorID == "010041")

               {

                   otp = utilities.newOTP();

                   Session["otp"] = otp;

               }

               else

               {

                   lblTest.Text = gt.Content.ServiceResponse.ServiceRespHeader.ErrorText.ToString();

                   string errorMessage = gt.Content.ServiceResponse.ServiceRespHeader.ErrorDetails.ToString();

                   MessageBox.Show(errorMessage);

               }

           }

           catch (Exception ex)

           {

               lblExceptionMsg.Text = ex.ToString();

           }

       }

   }

}



Download






Created with the Personal Edition of HelpNDoc: Easily create Web Help sites