Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Tratamento de exceção
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianoRC committed Oct 26, 2016
1 parent 499ef3b commit fb91e9d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 78 deletions.
15 changes: 15 additions & 0 deletions DotCep/DotCEP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@ Global
{A28B2C45-3DC2-46DD-9029-35EECFF84BF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A28B2C45-3DC2-46DD-9029-35EECFF84BF8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = None
$1.ResourceNamePolicy = FileFormatDefault
$0.VersionControlPolicy = $2
$2.inheritsSet = Mono
$0.TextStylePolicy = $3
$3.inheritsSet = null
$3.scope = application/config+xml
$0.XmlFormattingPolicy = $4
$4.inheritsSet = null
$4.scope = application/config+xml
version = 1.0.1
EndGlobalSection
EndGlobal
10 changes: 5 additions & 5 deletions DotCep/DotCep/Consultas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Endereco ObterEnderecoCompleto(uint CEP)

if (CEP.ToString().Length == 8)
{
String StrJSON = ControleJSON.ObterStringJSONS(ControleDeUrl.GerarURLDaPesquisa(CEP));
String StrJSON = ControleRequisicoes.ObterStringJSONS(ControleDeUrl.GerarURLDaPesquisa(CEP));

enderecoBase = JsonConvert.DeserializeObject<Endereco>(StrJSON);
}
Expand All @@ -29,14 +29,14 @@ public static Endereco ObterEnderecoCompleto(uint CEP)
/// Possíveis enderecos, utilizando o filtro de estado, cidade e logradouro,
/// </summary>
/// <returns>The lista de possiveis enderecos.</returns>
/// <param name="UF">U.</param>
/// <param name="UF">UF.</param>
/// <param name="Cidade">Cidade.</param>
/// <param name="logradouro">Logradouro.</param>
public static List<Endereco> ObterListaDeEnderecos(UF UF, String Cidade, String Logradouro)
{
List<Endereco> Enderecos = new List<Endereco>();
String url = ControleDeUrl.GerarURLDaPesquisa(UF, Cidade, Logradouro);
String StrJSON = ControleJSON.ObterStringJSONS(url);
String StrJSON = ControleRequisicoes.ObterStringJSONS(url);

Enderecos = JsonConvert.DeserializeObject<List<Endereco>>(StrJSON);

Expand All @@ -46,8 +46,8 @@ public static List<Endereco> ObterListaDeEnderecos(UF UF, String Cidade, String
/// <summary>
/// Obtem um CEP, mas só se as informações forem unicos e verdadeiros, se não ele retorna valores Empty.
/// </summary>
/// <returns>The CE.</returns>
/// <param name="UF">U.</param>
/// <returns>The CEP.</returns>
/// <param name="UF">UF.</param>
/// <param name="Cidade">Cidade.</param>
/// <param name="Logradouro">Logradouro.</param>
/// <param name="Formatado">If set to <c>true</c> formatado.</param>
Expand Down
25 changes: 0 additions & 25 deletions DotCep/DotCep/ControleJSON.cs

This file was deleted.

31 changes: 31 additions & 0 deletions DotCep/DotCep/ControleRequisicoes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net;
using System.IO;
using System.Text;

namespace DotCEP
{
internal static class ControleRequisicoes
{
internal static string ObterStringJSONS(string url)
{
try
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);

WebResponse response = request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
throw new System.Exception(string.Format("Erro ao tentar fazer a requisição: {0}", ex.Message));
}
}
}
}

4 changes: 3 additions & 1 deletion DotCep/DotCep/DotCEP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<RootNamespace>DotCEP</RootNamespace>
<AssemblyName>DotCEP</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<ReleaseVersion>1.0.1</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -35,7 +37,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Endereco.cs" />
<Compile Include="ControleJSON.cs" />
<Compile Include="ControleRequisicoes.cs" />
<Compile Include="UF.cs" />
<Compile Include="Consultas.cs" />
<Compile Include="Formatacao.cs" />
Expand Down
82 changes: 41 additions & 41 deletions DotCep/DotCep/Validacoes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@

namespace DotCEP
{
public static class Validacoes
{
public static bool VerificarValidadeDoCep(string CEP)
{
if (CEP.Trim().Length == 9)
{
return System.Text.RegularExpressions.Regex.IsMatch(CEP, ("[0-9]{5}-[0-9]{3}"));
}
else if (CEP.Trim().Length == 8)
{
CEP = Formatacao.FormatarCEP(CEP);
return System.Text.RegularExpressions.Regex.IsMatch(CEP, ("[0-9]{5}-[0-9]{3}"));
}
else
{
return false;
}
}
public static class Validacoes
{
public static bool VerificarValidadeDoCep(string CEP)
{
if (CEP.Trim().Length == 9)
{
return System.Text.RegularExpressions.Regex.IsMatch(CEP, ("[0-9]{5}-[0-9]{3}"));
}
else if (CEP.Trim().Length == 8)
{
CEP = Formatacao.FormatarCEP(CEP);
return System.Text.RegularExpressions.Regex.IsMatch(CEP, ("[0-9]{5}-[0-9]{3}"));
}
else
{
return false;
}
}

public static bool VerificarExistenciaDoCEP(string CEP)
{
uint CEPsemFormato;
public static bool VerificarExistenciaDoCEP(string CEP)
{
uint CEPsemFormato;

if (VerificarValidadeDoCep(CEP))
{
CEP = CEP.Replace("-", "");
if (VerificarValidadeDoCep(CEP))
{
CEP = CEP.Replace("-", "");

CEPsemFormato = Convert.ToUInt32(CEP);
CEPsemFormato = Convert.ToUInt32(CEP);

String StrJSON = ControleJSON.ObterStringJSONS(ControleDeUrl.GerarURLDaPesquisa(CEPsemFormato));
String StrJSON = ControleRequisicoes.ObterStringJSONS(ControleDeUrl.GerarURLDaPesquisa(CEPsemFormato));

if (!StrJSON.Contains("\"erro\": true"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
if (!StrJSON.Contains("\"erro\": true"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}

12 changes: 6 additions & 6 deletions Exemplos/Exemplos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class MainClass
{
public static void Main(string[] args)
{
ConsultarListaDeEnderecos(UF.RS, "Porto Alegre", "Olavo");
ConsultarCEP(UF.RS, "Pelotas", "Avenida Saldanha Marinho", true);
//ConsultarListaDeEnderecos(UF.RS, "Porto Alegre", "Olavo");
//ConsultarCEP(UF.RS, "Pelotas", "Avenida Saldanha Marinho", true);
ObterEndereco(70160900);

ValidarCEP("70160-900");
VerificarExistenciaCEP("70160900");
//ValidarCEP("70160-900");
//VerificarExistenciaCEP("70160900");

FormatarCEP(70160900);
FormatarCEP("70160900");
//FormatarCEP(70160900);
//FormatarCEP("70160900");

Console.ReadKey();
}
Expand Down

0 comments on commit fb91e9d

Please sign in to comment.