fonte: http://www.devmedia.com.br/articles/viewcomp.asp?comp=7783 | |
Ai galera uma funçãozinha formatada para vocês usarem em suas aplicações para consultar o CEP dos correios... Essa função retorna um TStrings onde cada linha contém uma informação do CEP como Rua, Bairro, UF, Endereço OBS: Acrescente no USES NMHTTP, NMURL. // criação da função function buscaCEP(lcCEP: string): TStringList; var loHTTP : TNMHTTP; loURL : TNMURL; lnCont : integer; begin try loHTTP := TNMHTTP.Create(Application); loURL := TNMURL.Create(Application); try loHTTP.Get('http://republicavirtual.com.br/web_cep.php?cep='+lcCEP+'&formato=query_string'); // retorna o conteudo da string e formata-o loURL.InputString := loHTTP.Body; // usa o separador de quebra de linha result := TStringList.Create; result.Text := UpperCase(StringReplace(loURL.Decode, '&', #13#10, [rfreplaceAll])); for lnCont := result.Count - 1 downto 0 do if trim(result[lnCont]) = '' then result.Delete(lncont); finally loHTTP.Free; loURL.Free; end; except result := nil; end; end; // exemplo var loCEP: TStringList; begin loCEP := Geral.buscaCEP(RetiraCharsAlfa(_edtCEP.Text)); if loCEP = nil then MensagemOK('CEP não localizado ou não há conexão com a Internet!!!') else if loCEP.Values['RESULTADO'] <> '1' then MensagemOK('CEP não localizado!!!') else begin _edtEndereco.Text := loCEP.Values['TIPO_LOGRADOURO'] + ' ' + loCEP.Values['LOGRADOURO']; _edtBairro .Text := loCEP.Values['BAIRRO']; _edtCidade .Text := loCEP.Values['CIDADE']; _edtEstado.ItemIndex := _edtEstado.Items.IndexOf(loCEP.Values['UF']); end; end; |
This entry was posted
on 07:31
and is filed under
DELPHI
.
You can leave a response
and follow any responses to this entry through the
Assinar:
Postar comentários (Atom)
.