Example #1
0
 /// <summary>
 /// Raises the <see cref="HttpRequest.ResponseReceived"/> event.
 /// </summary>
 /// <param name="sender">The <see cref="HttpRequest"/> object that raised the event</param>
 /// <param name="response">The <see cref="HttpResponse"/> object that the web server returned</param>
 internal void OnResponseReceivedEvent(HttpRequest sender, HttpResponse response)
 {
     if (_OnReceived == null)
     {
         _OnReceived = new ResponseHandler(OnResponseReceivedEvent);
     }
     if (Program.CheckAndInvoke(_ResponseReceived, _OnReceived, sender, response))
     {
         _ResponseReceived(this, response);
     }
 }
Example #2
0
        ///<summary>
        /// Synchronously requests a server with the specified parameters.  
        /// This should not be called directly. Instead, WaitForResponse 
        /// should be used. This method provides the acutal implementation 
        /// for the async request
        ///</summary>
        private void HandleRequestSync()
        {
            try
            {
                lock (_Lock)
                {
                    // if response was already made return and raise event
                    if (IsReceived)
                    {
                        OnResponseReceivedEvent(this, response);
                        return;
                    }

                    // add RawContentBytes to URL if http method is GET or DELETE
                    if (content != null)
                    {
                        if (HttpRequestMethod == RequestMethod.GET)
                        {
                            URL += ((GETContent)content).stringContent;
                        }
                        else if (HttpRequestMethod == RequestMethod.DELETE)
                        {
                            URL += ((DELETEContent)content).stringContent;
                        }
                    }

                    // build up the required message 
                    HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;

                    // set the proxy server
                    if (Proxy != null)
                    {
                        webRequest.Proxy = Proxy;
                    }

                    // add certificates to the request 
                    //if (certs.Count > 0)
                    //{
                    //    request.HttpsAuthentCerts = (X509Certificate[])certs.ToArray(typeof(X509Certificate));
                    //}

                    webRequest.Method = RequestMethodString[(int)HttpRequestMethod];

                    webRequest.ProtocolVersion = HttpVersion.Version11;

                    if (Accepts != null)
                    {
                        webRequest.Accept = Accepts;
                    }

                    if (ContentType != null)
                    {
                        webRequest.ContentType = ContentType;
                    }

                    if (UserAgent != null)
                    {
                        webRequest.UserAgent = UserAgent;
                    }

                    ICollection keys = AdditionalHeaderFieldsTable.Keys;
                    foreach (string key in keys)
                    {
                        webRequest.Headers.Set(key, (string)AdditionalHeaderFieldsTable[key]);
                    }

                    // Adding RawContentBytes to http request, no RawContentBytes body allowed for GET and DELETE, but URL Parameters 
                    // RawContentBytes can be null for GET and DELETE. This is also possible for for PUT and POST, but should not occur
                    if (content != null)
                    {
                        if (HttpRequestMethod == RequestMethod.POST)
                        {
                            if (((POSTContent)(content)).byteContent != null)
                            {
                                webRequest.ContentLength = ((POSTContent)(content)).byteContent.Length;
                                Stream reqStream = webRequest.GetRequestStream();
                                reqStream.Write(((POSTContent)(content)).byteContent, 0, ((POSTContent)(content)).byteContent.Length);
                                reqStream.Close();
                            }
                            else
                            {
                                webRequest.ContentLength = 0;
                            }
                        }
                        else if (HttpRequestMethod == RequestMethod.PUT)
                        {
                            if (((PUTContent)(content)).byteContent != null)
                            {
                                webRequest.ContentLength = ((PUTContent)(content)).byteContent.Length;
                                Stream reqStream = webRequest.GetRequestStream();
                                reqStream.Write(((PUTContent)(content)).byteContent, 0, ((PUTContent)(content)).byteContent.Length);
                                reqStream.Close();
                            }
                            else
                            {
                                webRequest.ContentLength = 0;
                            }
                        }
                    }

                    //compute the response and fill HttpResponse class with arguements 
                    HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                    if (webResponse.StatusCode == HttpStatusCode.OK)
                    {
                        // read data from response 
                        Stream stream = webResponse.GetResponseStream();

                        byte[] byteData = new byte[4096];
                        int temp = 0;

                        MemoryStream memoryStream = new MemoryStream();

                        while ((temp = stream.Read(byteData, 0, byteData.Length)) > 0)
                        {
                            if (temp == 0)
                            {
                                break;
                            }
                            memoryStream.Write(byteData, 0, temp);
                        }
                        stream.Close();
                        memoryStream.Close();

                        ContentType = webResponse.GetResponseHeader("Content-Type");
                        response = new HttpResponse(webResponse.StatusCode.ToString(), memoryStream.ToArray(), URL, webResponse.Headers, ContentType);
                    }
                    else
                    {
                        // request failed, but still record some of the response information to pass response received event
                        response = new HttpResponse(webResponse.StatusCode.ToString(), null, URL, webResponse.Headers, ContentType);
                    }
                    IsReceived = true;
                    webResponse.Close();
                }
                OnResponseReceivedEvent(this, response);
            }
            catch (Exception e)
            {
                Debug.Print("");
                Debug.Print("An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.");
                Debug.Print("");
                Debug.Print(e.Message);
            }
        }
Example #3
0
        private void ServerResponseCallback(HttpRequest sender, HttpResponse response)
        {
            Debug.Print("Event: received response from server.");
            Debug.Print("Status code: " + response.StatusCode);
            Debug.Print("Content: " + response.Text);

            if (response.StatusCode != "200")
                return;

            var hashtable = JsonSerializer.DeserializeString(response.Text) as Hashtable;
            int status;
            int person;
            try
            {
                Debug.Print("Order to take care of ? " + hashtable["CurrentStatus"]);
                Debug.Print("Customer: " + hashtable["Person"]);
                status = Int32.Parse(hashtable["CurrentStatus"].ToString());
                person = Int32.Parse(hashtable["Person"].ToString());
            }
            catch (System.NullReferenceException ex)
            {
                Debug.Print("Invalid JSON response");
                return;
            }

            switch (status)
            {
                case (int)CurrentStatus.Idle:
                    Debug.Print("No order");
                    break;
                case (int)CurrentStatus.Busy:
                    switch (person)
                    {
                        case (int)People.Toby:
                            Debug.Print("Go to Toby");
                            train.Go(Stations.TobyStation);
                            break;
                        case (int)People.Tristram:
                            Debug.Print("Go to Tristram");
                            train.Go(Stations.TristramStation);
                            break;
                    }
                    break;
            }
        }
        void StatusPostsReceived(HttpRequest sender, HttpResponse response)
        {
            Debug.Print("Status Posts Recieved: ");
            Debug.Print(response.StatusCode);

            // Blank out the "Updating" message
            _loggerDisplay.ShowMessage("", 10, StatusYPosition);

            if (response.StatusCode == "200")
            {
                Debug.Print("OK");
                Debug.Print(response.Text);

                var statusPosts = Json.NETMF.JsonSerializer.DeserializeString(response.Text) as ArrayList;
                if (statusPosts != null)
                {
                    ProcessStatusMessages(statusPosts);
                }
            }
        }
Example #5
0
        void request_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            switch (opcion)
            {
                case "tiempo":
                    textotemp.Text = "Esperando ...";
                    timer.Start();
                    respuesta = response.Text;
                    Double horaLocal = 0;
                    String horaStr = "";
                    XmlReader xmL = XmlReader.Create(response.Stream);
                    while (xmL.Read())
                    {
                        if ((xmL.NodeType == XmlNodeType.Element) && (xmL.Name == "timestamp"))
                        {

                            horaStr = xmL.ReadElementString();
                            horaLocal = Double.Parse(horaStr);
                            horaStr = UnixTimeStampToDateTime(horaLocal);

                        }

                    }
                    respuesta = horaStr;
                    String[] res = respuesta.Split(' ');
                    tiempo = horaLocal;
                    fechatext.Text = res[0];
                    textotemp.Text = res[1];

                    break;
                case "temperatura":

                    int celsius = 0;
                    Double C;
                    String celsiusStr = "";
                    String clima = "";
                    XmlReader xmLtemp = XmlReader.Create(response.Stream);
                    while (xmLtemp.Read())
                    {
                        if ((xmLtemp.NodeType == XmlNodeType.Element) && ((xmLtemp.Name == "temperature") || (xmLtemp.Name == "weather")))
                        {
                            if (xmLtemp.HasAttributes)
                            {
                                if (xmLtemp.Name == "temperature")
                                {
                                    celsiusStr = xmLtemp.GetAttribute("value");
                                    C = Double.Parse(celsiusStr);
                                    celsius = (int)(C - 273.15);
                                }
                                else if (xmLtemp.Name == "weather")
                                {
                                    clima = xmLtemp.GetAttribute("value");
                                }

                            }
                        }

                    }
                    temp.Text = celsius.ToString();
                    msm.Text = clima;

                    break;
                case "noticias":

                    XmlReader rssXmlDoc = XmlReader.Create(response.Stream);
                    while (rssXmlDoc.Read())
                    {
                        if ((rssXmlDoc.NodeType == XmlNodeType.Element) && (rssXmlDoc.Name == "item"))
                        {
                            intnoticias=0;
                            ix = 0;
                            while (rssXmlDoc.Read())
                            {

                                if ((rssXmlDoc.NodeType == XmlNodeType.Element) && (rssXmlDoc.Name == "title"))
                                {
                                    if (ix < 10)
                                    {
                                        s2[ix] = rssXmlDoc.ReadElementString();

                                        ix++;
                                    }
                                }

                            }

                        }

                    }
                    Debug.Print(removerAcentos(s2[intnoticias]));
                    n1.Text = removerAcentos(s2[intnoticias]);
                    n2.Text = removerAcentos(s2[intnoticias+1]);

                    break;
                default:
                    break;

            }
        }
Example #6
0
        private void handshake_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            websocketConnectionUp = false;
            Debug.Print("got response:");
            Debug.Print(response.Text);
            string[] parts = (response.Text).Split(':');
            if (parts.Length < 4)
            {
                throw new Exception("Error in socket.io handshake - wrong number of arguments received!");
            }
            else
            {
                session_id = parts[0];
                Debug.Print("Session ID: " + session_id);

                // updates the new session id to the uri
                update_socketio_uri();

                // sends connect through ws
                websocketConnectionUp = true;
                send_connect();
            }
        }
 void req_ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     UserInterface.Instance.log(response.Text);
 }
Example #8
0
        void req_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            Debug.Print("SolarPulse - Data sent");

            lightTimer.Start();
        }
 void hr_ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     throw new NotImplementedException();
 }
 void client_ResponseReceived2(HttpRequest sender, HttpResponse response)
 {
     //Nothing to do here
 }
Example #11
0
 void request_ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     respuesta = response.Text;
     Debug.Print(respuesta);
     // btn_inicio.Text = respuesta;
     textotemp.Text = respuesta;
     barra.Value = (int)Double.Parse(respuesta);
     Glide.MainWindow = pantallaTemperatura;
 }
Example #12
0
        void pedido_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            //if (response.StatusCode == "202")         {

                var resultado = response.Text;

                Debug.Print(resultado);

            /*
                String r = GetUrlFromJson(resultado.ToString());
                if (r != String.Empty && r.Length > 2 && r != "null")
                {
                    GHI.Glide.UI.TextBlock text = (GHI.Glide.UI.TextBlock)resultWindow.GetChildByName("TxtResult");
                    text.Text = r;
                    Glide.MainWindow = resultWindow;
                }
                else
                {
                    timerPictureCaptured.Start();
                }

            */
                //HttpRequest pedido = HttpHelper.CreateHttpGetRequest(GetUrlFromJson(resultado.ToString()));
                //pedido.ResponseReceived += pedido_ResponseReceived;
                //pedido.SendRequest();
                //Debug.Print(resultado);
                //Debug.Print("Enviando Url de QR");
            //}
        }
Example #13
0
 private void MeasurementPostCompleted(HttpRequest sender, HttpResponse response)
 {
     if (response.StatusCode == "201")
     {
         _loggerDisplay.ClearMessage(0, StatusYPosition);
         Debug.Print("Measurements send OK.");
     }
     else
     {
         _loggerDisplay.ShowErrorMessage("Error: " + response.StatusCode, 10, StatusYPosition);
         Debug.Print("HttpCode: " + response.StatusCode);
         Debug.Print("Error :" + response.Text);
     }
 }
Example #14
0
 void req_ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     DisplayText(4, "Response received(200 is OK): " + response.StatusCode);
 }
Example #15
0
        void TestResponseReceived(HttpRequest sender, HttpResponse response)
        {
            if (response.StatusCode.Substring(0, 3) == "200")
            {
                multicolorLed.FadeOnce(GT.Color.White, new TimeSpan(0, 0, 1), GT.Color.Green);
            }
            else
            {
                multicolorLed.FadeOnce(GT.Color.Black, new TimeSpan(0, 0, 1), GT.Color.Green);
            }

            Debug.Print("Test web request: response status " + response.StatusCode);
        }
 private static void ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     Debug.Print("Network Response");
     Debug.Print("Status Code: " + response.StatusCode);
     //Debug.Print("Text: " + response.Text);
 }
Example #17
0
 void request_ResponseReceived(HttpRequest sender, HttpResponse response)
 {
     var x = response.Text;
     Debug.Print(x);
     temp = (TextBlock)Glide.MainWindow.GetChildByName("texto_temperatura");
     temp.Text = x;
     sliderBar = (Slider)Glide.MainWindow.GetChildByName("slider");
     sliderBar.Value = Convert.ToDouble(x);
     Glide.MainWindow = temperatura;
 }