Ejemplo n.º 1
0
        public RetornoCEP CEPNovo(string cep)
        {
            RetornoCEP retorno = new RetornoCEP();

            cep = cep.Replace(".", "");

            Enderecos end = new EnderecosDB().Buscar(cep);

            if (end == null)
            {
                retorno.endereco = "";
                retorno.bairro   = "";
                retorno.cidade   = "";
                retorno.estado   = "";
                retorno.codibge  = "";
            }
            else
            {
                retorno.endereco = end.logradouro.ToString();
                retorno.bairro   = end.bairro.ToString();
                retorno.cidade   = end.cidade.ToString();
                retorno.estado   = end.uf.ToString();
                retorno.codibge  = end.ibge_cod_cidade.ToString();
            }


            return(retorno);
        }
Ejemplo n.º 2
0
        public RetornoCEP CEP(string cep)
        {
            RetornoCEP retorno = new RetornoCEP();

            cep = cep.Replace(".", "").Replace("-", "");

            if (cep.Length == 8)
            {
                HttpWebRequest  requisicao = (HttpWebRequest)WebRequest.Create(String.Format("https://viacep.com.br/ws/{0}/json/unicode", cep));
                HttpWebResponse resposta   = (HttpWebResponse)requisicao.GetResponse();
                int             cont;
                byte[]          buffer = new byte[1000];
                StringBuilder   sb     = new StringBuilder();
                string          temp;
                Stream          stream = resposta.GetResponseStream();
                do
                {
                    cont = stream.Read(buffer, 0, buffer.Length);
                    temp = Encoding.Default.GetString(buffer, 0, cont).Trim();
                    sb.Append(temp);
                } while (cont > 0);

                if (sb.ToString().IndexOf("\"erro\": true") == -1)
                {
                    JsonTextReader reader = new JsonTextReader(new StringReader(sb.ToString()));
                    string         titulo = "";
                    while (reader.Read())
                    {
                        if (reader.Value != null)
                        {
                            if (reader.TokenType.ToString() == "PropertyName")
                            {
                                titulo = reader.Value.ToString();
                            }

                            if (reader.TokenType.ToString() == "String")
                            {
                                if (titulo == "logradouro")
                                {
                                    retorno.endereco = reader.Value.ToString();
                                }
                                if (titulo == "bairro")
                                {
                                    retorno.bairro = reader.Value.ToString();
                                }
                                if (titulo == "localidade")
                                {
                                    retorno.cidade = reader.Value.ToString();
                                }
                                if (titulo == "uf")
                                {
                                    retorno.estado = reader.Value.ToString();
                                }
                                if (titulo == "ibge")
                                {
                                    retorno.codibge = reader.Value.ToString();
                                }
                            }
                        }
                    }
                }
            }


            return(retorno);
        }