Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

inserido localidade no JSON result

  • Loading branch information...
commit 07d7fc3ba8046cbcd60d1fb7af9162ba2bea5c13 1 parent ff3f17b
Abraao Alves authored
View
BIN  ConsultaCEP.sln.docstates
Binary file not shown
View
BIN  ConsultaCEP.suo
Binary file not shown
View
3  ConsultaCEP/ConsultaCEP.csproj
@@ -36,6 +36,9 @@
<Reference Include="EntityFramework">
<HintPath>..\packages\EntityFramework.4.1.10331.0\lib\EntityFramework.dll</HintPath>
</Reference>
+ <Reference Include="HtmlAgilityPack, Version=1.4.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
+ <HintPath>..\dependencias\HtmlAgilityPack.dll</HintPath>
+ </Reference>
<Reference Include="System.Data.Entity" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
View
58 ConsultaCEP/Controllers/ConsultaController.cs
@@ -10,24 +10,24 @@
using System.Xml;
using System.Dynamic;
+using HtmlAgilityPack;
+
namespace ConsultaCEP.Controllers
{
public class CepController : Controller
{
- //
- // GET: /Cep/
-
public JsonResult Consulta(string cep)
{
var sb = new StringBuilder();
var buf = new byte[8192];
- Uri uri = new Uri("http://www.buscacep.correios.com.br/servicos/dnec/consultaLogradouroAction.do?CEP=" + cep + "&Metodo=listaLogradouro&TipoConsulta=cep&StartRow=1&EndRow=10");
+ Uri uri = new Uri("http://m.correios.com.br/movel/buscaCepConfirma.do?cepEntrada=" + cep +"&tipoCep=&cepTemp=&metodo=buscarCep");
HttpWebRequest request = (HttpWebRequest)
WebRequest.CreateDefault(uri);
-
+ request.Method = "POST";
+
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
@@ -48,29 +48,31 @@ public JsonResult Consulta(string cep)
}
while (count > 0);
- sb.Insert(0, "<root>");
- sb.Remove(6, 12380);
- sb.Remove(311, sb.Length - 311);
- sb.Append("</root>");
-
-
- var xml = XElement.Parse(sb.ToString());
-
- dynamic endereco = new ExpandoObject();
-
- if (xml.Elements().ToList().Count == 0)
- {
- endereco.Mensagem = "Cep não encontrado!";
- return Json(endereco, JsonRequestBehavior.AllowGet);
- }
-
- endereco.Rua = xml.Elements().ToList()[0].Value;
- endereco.Bairro = xml.Elements().ToList()[1].Value;
- endereco.Cidade = xml.Elements().ToList()[2].Value;
- endereco.Estado = xml.Elements().ToList()[3].Value;
- endereco.Mensagem = "Sucesso";
-
- return Json(endereco, JsonRequestBehavior.AllowGet);
+ HtmlDocument doc = new HtmlDocument();
+ doc.LoadHtml(sb.ToString());
+
+ var form = doc.GetElementbyId("frmCep");
+
+ if (form == null)
+ return Json(new { Mensagem = "Cep não encontrado!" }, JsonRequestBehavior.AllowGet);
+
+ var elementos = form.SelectSingleNode("//div[@class='caixacampobranco']");
+
+ var result = new List<string>();
+
+ foreach (var item in elementos.SelectNodes("//span[@class='respostadestaque']"))
+ result.Add(item.InnerText.Trim().Replace("\n", "").Replace("\t", "").Replace(" ", ""));
+
+ return Json(new {
+ Logradouro = result[0],
+ Bairro = result[1],
+ Localidade = new {
+ Cidade = result[2].Split('/')[0],
+ Estado = result[2].Split('/')[1]
+ },
+ Cep = result[3],
+ Mensagem = (result[3] != cep) ? "Cep Modificado" : "Sucesso"
+ }, JsonRequestBehavior.AllowGet);
}
}
View
BIN  ConsultaCEP/bin/ConsultaCEP.dll
Binary file not shown
View
BIN  ConsultaCEP/bin/ConsultaCEP.pdb
Binary file not shown
View
BIN  ConsultaCEP/bin/HtmlAgilityPack.dll
Binary file not shown
View
8 ConsultaCEP/obj/Debug/ConsultaCEP.csproj.FileListAbsolute.txt
@@ -5,3 +5,11 @@ c:\users\nielsenteixeira\documents\visual studio 2010\Projects\ConsultaCEP\Consu
c:\users\nielsenteixeira\documents\visual studio 2010\Projects\ConsultaCEP\ConsultaCEP\obj\Debug\ResolveAssemblyReference.cache
c:\users\nielsenteixeira\documents\visual studio 2010\Projects\ConsultaCEP\ConsultaCEP\obj\Debug\ConsultaCEP.dll
c:\users\nielsenteixeira\documents\visual studio 2010\Projects\ConsultaCEP\ConsultaCEP\obj\Debug\ConsultaCEP.pdb
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\bin\ConsultaCEP.dll
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\bin\ConsultaCEP.pdb
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\bin\EntityFramework.dll
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\bin\HtmlAgilityPack.dll
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\bin\EntityFramework.xml
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\obj\Debug\ResolveAssemblyReference.cache
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\obj\Debug\ConsultaCEP.dll
+C:\Projetos\Cep\Consulta-CEP-Correios\ConsultaCEP\obj\Debug\ConsultaCEP.pdb
View
BIN  ConsultaCEP/obj/Debug/ConsultaCEP.dll
Binary file not shown
View
BIN  ConsultaCEP/obj/Debug/ConsultaCEP.pdb
Binary file not shown
View
BIN  ConsultaCEP/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
Binary file not shown
View
BIN  ConsultaCEP/obj/Debug/ResolveAssemblyReference.cache
Binary file not shown
View
BIN  dependencias/HtmlAgilityPack.dll
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.