- 1 Abril 2011
- Sem categoria
- 0 Comments
Pois é, a TDT já anda aí, e por razões que não interessam, precisei de retornar todos os instaladores por cada código postal, cuja PT indica como sendo um instalador habilitado.
Para tal, fiz uma pequena aplicação que enviada um pedido com o código postal, e interpretava o resultado retornado em JSON.
Primeiro tive que criar um ficheiro de texto com todos os códigos postais. Podem fazer download do que usei aqui.
O código poderá não ser o melhor, mas fez o que pretendia e não precisei de me chatear mais com o assunto.
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
namespace WebApplication1
{
public class CPs
{
public string cp4 { get; set; }
public string cp3 { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
private void GetData()
{
List<CPs> cps = new List<CPs>();
TextReader t = new StreamReader(@"C:cps.txt");
string line = "";
t.ReadLine();
while (t.Peek() > 0)
{
line = t.ReadLine();
cps.Add(new CPs() { cp4 = line.Split('-')[0].ToString(), cp3 = line.Split('-')[1].ToString() });
}
HttpWebRequest request = null;
foreach (CPs cp in cps)
{
try
{
request = (HttpWebRequest)HttpWebRequest.Create(@"http://tdt.telecom.pt/handlers/installerSearch.ashx?cp4=" + cp.cp4 + "&cp3=" + cp.cp3);
request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json, text/javascript, */*";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
string json = "";
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
json += reader.ReadLine();
}
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> x = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (x.Count > 0)
Response.Write(cp.cp4 + ";" + cp.cp3 + ";" + x["name"].ToString() + ";" + x["phoneNumber1"].ToString() + ";" + x["phoneNumber2"].ToString() + "<br />");
}
catch (Exception)
{
}
}
}
}
}







