Exemple #1
0
        void GetResponseCallback(IAsyncResult result)
        {
            HttpWebRequest request = result.AsyncState as HttpWebRequest;

            if (request != null)
            {
                try
                {
                    WebResponse  response       = request.EndGetResponse(result);
                    Stream       streamResponse = response.GetResponseStream();
                    StreamReader streamRead     = new StreamReader(streamResponse);
                    string       responseString = streamRead.ReadToEnd();
                    responseString = responseString.Trim();



                    if (!string.IsNullOrEmpty(responseString))
                    {
                        try
                        {
                            var resultsJSON = JsonConvert.DeserializeObject <List <RootObject> >(responseString);

                            foreach (var rr in resultsJSON.ToList())
                            {
                                listBoxData adata = new listBoxData();
                                adata.dealTitle   = rr.title;
                                adata.dealPrice   = "Rs." + rr.current_price + "(" + rr.off_percent + "% off)";
                                adata.dealOgPrice = rr.original_price;
                                adata.dealTime    = rr.created_at;
                                DateTime datetime = Convert.ToDateTime(rr.created_at);
                                adata.dealTime = DateTimeAgo.TimeAgo(datetime);

                                if (!string.IsNullOrEmpty(rr.image_thumb))
                                {
                                    adata.imgThumb = rr.image_thumb;
                                }
                                else
                                {
                                    adata.imgThumb = "/Assets/noimage.jpg";
                                }

                                Dispatcher.BeginInvoke(() =>
                                {
                                    lstDeal.Items.Add(adata);
                                });
                            }
                            PClass.SetProgressIndicator(false);
                        }
                        catch (Exception e)
                        {
                            PClass.SetProgressIndicator(false);
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show(e.Message);
                            });
                        }
                    }
                    // Close the stream object
                    streamResponse.Close();
                    streamRead.Close();

                    // Release the HttpWebResponse
                    response.Close();

                    //Do what you want with this response
                }
                catch (WebException e)
                {
                    PClass.SetProgressIndicator(false);
                    // You will get detail reason of exception(if there) in text variable.
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                        using (Stream data = response.GetResponseStream())
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                //string text = new StreamReader(data).ReadToEnd();
                                MessageBox.Show(e.Status.ToString());
                            });
                        }
                    }
                    return;
                }
                catch (Exception e)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //string text = new StreamReader(data).ReadToEnd();
                        MessageBox.Show(e.InnerException.ToString());
                    });
                }
            }
        }
        void GetResponseCallback(IAsyncResult result)
        {
            HttpWebRequest request = result.AsyncState as HttpWebRequest;
            if (request != null)
            {
                try
                {
                    WebResponse response = request.EndGetResponse(result);
                    Stream streamResponse = response.GetResponseStream();
                    StreamReader streamRead = new StreamReader(streamResponse);
                    string responseString = streamRead.ReadToEnd();
                    responseString = responseString.Trim();

                    if (!string.IsNullOrEmpty(responseString))
                      {
                          try
                          {
                              var resultsJSON = JsonConvert.DeserializeObject<List<RootObject>>(responseString);

                              foreach (var rr in resultsJSON.ToList())
                              {
                                  listBoxData adata = new listBoxData();
                                  adata.dealTitle = rr.title;
                                  adata.dealPrice = "Rs." + rr.current_price + "(" + rr.off_percent + "% off)";
                                  adata.dealOgPrice = rr.original_price;
                                  adata.dealTime = rr.created_at;
                                  DateTime datetime = Convert.ToDateTime(rr.created_at);
                                  adata.dealTime = DateTimeAgo.TimeAgo(datetime);

                                  if (!string.IsNullOrEmpty(rr.image_thumb))
                                  {
                                      adata.imgThumb = rr.image_thumb;

                                  }
                                  else
                                  {

                                      adata.imgThumb = "/Assets/noimage.jpg";
                                  }

                                  Dispatcher.BeginInvoke(() =>
                                  {
                                      lstDeal.Items.Add(adata);
                                  });

                              }
                              PClass.SetProgressIndicator(false);
                          }
                          catch (Exception e) {
                              PClass.SetProgressIndicator(false);
                              Dispatcher.BeginInvoke(() =>
                              {
                                  MessageBox.Show(e.Message);
                              });
                             }
                      }
                    // Close the stream object
                    streamResponse.Close();
                    streamRead.Close();

                    // Release the HttpWebResponse
                    response.Close();

                    //Do what you want with this response
                }
                catch (WebException e)
                {

                    PClass.SetProgressIndicator(false);
                   // You will get detail reason of exception(if there) in text variable.
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                        using (Stream data = response.GetResponseStream())
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                //string text = new StreamReader(data).ReadToEnd();
                                MessageBox.Show(e.Status.ToString());
                            });

                        }
                    }
                    return;
                }
                catch (Exception e)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //string text = new StreamReader(data).ReadToEnd();
                        MessageBox.Show(e.InnerException.ToString());
                    });
                }
            }
        }