Exemplo n.º 1
0
        public static async Task Tracking()
        {
            try
            {
                ShipmentTracking response    = new ShipmentTracking();
                EnviaYa          request     = new EnviaYa();
                string           urlTracking = ConfigurationManager.AppSettings["urlTracking"].ToString();

                request.api_key         = ConfigurationManager.AppSettings["api_key"].ToString();
                request.carrier         = "FedEx";
                request.shipment_number = "TEST0000FK44E5Y7"; //enviaya_shipment_number en ShipmentResponse

                var json = JsonConvert.SerializeObject(request);
                var data = new StringContent(json, Encoding.UTF8, "application/json");

                var client = new HttpClient();
                //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "2212d26b11259e906d326a62405642c7");

                var responseResult = await client.PostAsync(urlTracking, data);

                if (response != null)
                {
                    var results = responseResult.Content.ReadAsStringAsync().Result;

                    if (!string.IsNullOrEmpty(results))
                    {
                        var shipmentResponse = JsonConvert.DeserializeObject <ShipmentTracking>(results);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public int ActualizaStatusEntregado()
        {
            JsonReader     readerJson = null;
            JsonSerializer serilizer  = null;
            StreamReader   sr         = null;
            int            response   = 0;
            string         query      = ConfigurationManager.AppSettings["ActualizaStatusEntregado"].ToString();
            List <EnviaYa> envia      = new List <EnviaYa>();

            string urlTracking = ConfigurationManager.AppSettings["urlTracking"].ToString();

            Console.WriteLine(urlTracking);
            var request = (HttpWebRequest)WebRequest.Create(urlTracking);

            request.Method = "POST";
            Stream dataStream = request.GetRequestStream();

            try
            {
                using (SqlConnection connection = new SqlConnection(cadena))
                {
                    SqlCommand command = new SqlCommand(query, connection);
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            EnviaYa env = new EnviaYa();
                            env.api_key         = ConfigurationManager.AppSettings["api_key"].ToString();
                            env.carrier_account = null;
                            env.OrdenId         = reader[0].ToString();
                            env.carrier         = reader[1].ToString();
                            env.shipment_number = reader[3].ToString();
                            envia.Add(env);
                        }
                    }
                    connection.Close();
                    connection.Dispose();
                }

                foreach (EnviaYa item in envia)
                {
                    string jSonToSend = JsonConvert.SerializeObject(item);
                    var    data       = Encoding.ASCII.GetBytes(jSonToSend);

                    using (var stream = dataStream)
                    {
                        stream.Write(data, 0, data.Length);
                    }

                    var    responseEnv    = (HttpWebResponse)request.GetResponse();
                    String responseString = new StreamReader(responseEnv.GetResponseStream()).ReadToEnd();
                    //TODO: Actualizar el estaus de cada orden
                }
            }
            catch (Exception ex)
            {
                string mess = ex.Message;
                Console.WriteLine(ex.Message);
                throw;
            }

            return(response);
        }