Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Net;
- using System.Net.Sockets;
- using System.Windows.Forms;
- using System.IO;
- using System.Threading;
- using System.Net.Security;
- namespace EndereçoPeloCep
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- /**
- *
- * Verifica se o cep digitado é válido.
- *
- */
- Match regex = Regex.Match(textBox1.Text, "^[0-9]{8}$");
- /**
- *
- * Se o CEP digitado for valido...
- *
- */
- if (regex.Success)
- {
- listBox1.Items.Clear();
- try
- {
- /**
- *
- * CEP a ser pesquisado
- *
- */
- string cep = textBox1.Text;
- /**
- *
- * Cria a requisição
- *
- */
- HttpWebRequest Request =
- (HttpWebRequest)WebRequest.Create("http://www.buscacep.correios.com.br/servicos/dnec/consultaEnderecoAction.do");
- /**
- *
- * Define o que será postado
- *
- */
- string postData = "relaxation=" + cep + "&TipoCep=ALL&semelhante=N&Metodo=listaLogradouro&TipoConsulta=relaxation&StartRow=1&EndRow=10&cfm=1";
- /**
- *
- * Converte a string de post para um ByteStream
- *
- */
- byte[] postBytes = Encoding.ASCII.GetBytes(postData);
- /**
- *
- * Parâmetros da requisição
- *
- */
- Request.Method = "POST";
- Request.ContentType = "application/x-www-form-urlencoded";
- Request.ContentLength = postBytes.Length;
- Stream requestStream = Request.GetRequestStream();
- /**
- *
- * Envia Requisição
- *
- */
- requestStream.Write(postBytes, 0, postBytes.Length);
- requestStream.Close();
- /**
- *
- * Resposta do servidor dos correios
- *
- */
- HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
- listBox1.Items.Add("Resposta do Servidor: " + response.StatusCode.ToString());
- /**
- *
- * String com a resposta do servidor
- *
- */
- string responseText = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
- /**
- *
- * Separa os dados com expressão regular
- *
- */
- MatchCollection matches = Regex.Matches(responseText, ">(.*?)</td>");
- /**
- *
- * Exibe os dados recebidos
- *
- */
- UTF8Encoding utf8 = new UTF8Encoding();
- /**
- *
- * @ RUA
- * Passa o texto convertido para a textBox
- *
- */
- textBox2.Text = matches[0].Groups[1].ToString();
- /**
- *
- * @Bairro
- *
- */
- textBox3.Text = matches[1].Groups[1].ToString();
- /**
- *
- * @Cidade
- *
- */
- textBox4.Text = matches[2].Groups[1].ToString();
- /**
- *
- * @Estado
- *
- */
- textBox5.Text = matches[3].Groups[1].ToString();
- }
- catch (Exception ex)
- {
- listBox1.Items.Clear();
- listBox1.Items.Add("! Erro na execução: " + ex.ToString());
- }
- }
- else
- {
- listBox1.Items.Clear();
- listBox1.Items.Add("CEP inválido");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement