Пример #1
0
 /// <summary>
 /// This method sends a Signal to an active Session.
 /// </summary>
 private void SendSignal()
 {
     try
     {
         SendSignalResponse signalResponse = this.requestFactory.SendSignal(lblSessionId.Text, ddlSignal.SelectedValue);
         if (null != signalResponse)
         {
             NameValueCollection displayParam = new NameValueCollection();
             displayParam.Add("status", signalResponse.Status);
             this.DrawPanelForSuccess(pnlSendSignal, displayParam, string.Empty);
         }
         else
         {
             this.DrawPanelForFailure(pnlSendSignal, "Unable to send signal.");
         }
     }
     catch (ArgumentNullException ane)
     {
         this.DrawPanelForFailure(pnlSendSignal, ane.Message);
     }
     catch (InvalidScopeException ise)
     {
         this.DrawPanelForFailure(pnlSendSignal, ise.Message);
     }
     catch (InvalidResponseException ire)
     {
         this.DrawPanelForFailure(pnlSendSignal, ire.Body);
     }
     catch (Exception ex)
     {
         this.DrawPanelForFailure(pnlSendSignal, ex.Message);
     }
 }
    /// <summary>
    /// This method sends a Signal to an active Session.
    /// </summary>
    private void SendSignal()
    {
        try
        {
            string         sendSignalResponse;
            HttpWebRequest sendSignalRequestObject = (HttpWebRequest)System.Net.WebRequest.Create(string.Empty + this.endPoint + "/rest/1/Sessions/" + sessionIdOfCreateSessionResponse + "/Signals");
            string         strReq = "{\"signal\":\"" + signal.Value.ToString() + "\"}";
            sendSignalRequestObject.Method = "POST";
            sendSignalRequestObject.Headers.Add("Authorization", "Bearer " + this.accessToken);
            sendSignalRequestObject.ContentType = "application/json";
            sendSignalRequestObject.Accept      = "application/json";

            UTF8Encoding encoding  = new UTF8Encoding();
            byte[]       postBytes = encoding.GetBytes(strReq);
            sendSignalRequestObject.ContentLength = postBytes.Length;

            Stream postStream = sendSignalRequestObject.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            HttpWebResponse sendSignalResponseObject = (HttpWebResponse)sendSignalRequestObject.GetResponse();
            using (StreamReader sendSignalResponseStream = new StreamReader(sendSignalResponseObject.GetResponseStream()))
            {
                sendSignalResponse = sendSignalResponseStream.ReadToEnd();
                if (!string.IsNullOrEmpty(sendSignalResponse))
                {
                    JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
                    SendSignalResponse   deserializedJsonObj   = (SendSignalResponse)deserializeJsonObject.Deserialize(sendSignalResponse, typeof(SendSignalResponse));
                    if (null != deserializedJsonObj)
                    {
                        statusOfSendSignalResponse = deserializedJsonObj.status;
                    }
                    else
                    {
                        sendSignalErrorResponse = "Got response but not able to deserialize json " + sendSignalResponse;
                    }
                }
                else
                {
                    sendSignalErrorResponse = "No response from the gateway.";
                }

                sendSignalResponseStream.Close();
            }
        }
        catch (WebException we)
        {
            string errorResponse = string.Empty;

            try
            {
                using (StreamReader sr2 = new StreamReader(we.Response.GetResponseStream()))
                {
                    errorResponse = sr2.ReadToEnd();
                    sr2.Close();
                }
            }
            catch
            {
                errorResponse = "Unable to get response";
            }

            sendSignalErrorResponse = errorResponse + Environment.NewLine + we.ToString();
        }
        catch (Exception ex)
        {
            sendSignalErrorResponse = ex.ToString();
        }
    }