Exemplo de chamada a serviço REST (XML)
Segue, em anexo, para ajudar na resolução do Exercício O07!
exemplochamadaservicorest
// Cria o objeto de WebProxy, passando como parâmetro o endereço do proxy e a porta
System.Net.WebProxy proxyObject = new System.Net.WebProxy(TextBoxProxy.Text, Convert.ToInt32(TextBoxPorta.Text));
// Passa o login, a senha e o domínio do WebProxy
proxyObject.Credentials = new System.Net.NetworkCredential(TextBoxLogin.Text, TextBoxSenha.Text, TextBoxDominio.Text);
// Configurar o proxy para a requisição web
WebRequest.DefaultWebProxy = proxyObject;
// Configura o proxy
XmlReader reader = XmlReader.Create("http://news.google.com/news?ned=pt-PT_pt&q="+textBoxConsulta.Text+"&output=rss");
/* Exemplo de RSS:
* - <item>
<title>Unesco distingue Lula da Silva por contributo para a paz - Diário Digital</title>
<link>http://news.google.com/news/url?fd=R&id_news=395758&sa=T&url=http://diariodigital.sapo.pt/news.asp?section_id=10&usg=AFQjCNHKNP4y_yH2ocPSlclqAkMeHGozlA</link>
<guid isPermaLink="false">tag:news.google.com,2005:cluster=http://diariodigital.sapo.pt/news.asp?section_id=10&id_news=395758</guid>
<pubDate>Thu, 25 Jun 2009 14:35:33 GMT</pubDate>
<description><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?fd=R&sa=T&url=http://www.google.com/hostednews/afp/article/ALeqM5js42auha7bZRP1gpiB_4ZPg4TwFw&usg=AFQjCNHE8b4rzlv8GVVo694gobQssLuo5w"><img src="http://nt1.ggpht.com/news/tbn/HUr_ZEML-aBYBM/0.jpg" alt="" border="1" width="80" height="65" /><br /><font size="-2">AFP</font></a></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br /><div style="padding-top:0.8em;"><img alt="" height="1" width="1" /></div><div class="lh"><a href="http://news.google.com/news/url?fd=R&id_news=395758&sa=T&url=http://diariodigital.sapo.pt/news.asp?section_id=10&usg=AFQjCNHKNP4y_yH2ocPSlclqAkMeHGozlA"><b>Unesco distingue <b>Lula</b> da Silva por contributo para a paz</b></a><br /><font size="-1"><b><font color="#6f6f6f">Diário Digital</font></b></font><br /><font size="-1">O presidente brasileiro, Luiz Inácio <b>Lula</b> da Silva, vai receber a 7 de Julho, na sede da Unesco, em Paris, o Prémio Incentivo da Paz, Félix Houphouët-Boigny <b>...</b></font><br /><font size="-1"><a href="http://news.google.com/news/url?fd=R&sa=T&url=http://www.google.com/hostednews/afp/article/ALeqM5js42auha7bZRP1gpiB_4ZPg4TwFw&usg=AFQjCNHE8b4rzlv8GVVo694gobQssLuo5w"><b>Lula</b> receberá prêmio da Unesco por sua contribuição para a paz</a><font size="-1" color="#6f6f6f"><nobr>AFP</nobr></font></font><br /><font size="-1" class="p"></font><br /><font class="p" size="-1"><a class="p" href="http://news.google.com/news?ned=pt-PT_pt&hl=pt&ncl=dv3fdR8kmqucBcMpM_OiTkRF-pEPM"><nobr><b>todos os 15 artigos de notícias »</b></nobr></a></font></div></font></td></tr></table></description>
</item> */
// Percorra cada nó de um arquivo XML
while (reader.Read())
{
// Se encontrar o nó do tipo item
if((reader.Name=="item") && (reader.NodeType== XmlNodeType.Element))
{
// Encontra o title
reader.ReadToDescendant("title");
// Lê o conteúdo do title
reader.Read();
// Exibe o valor do title
TextBoxResultado.Text += "* " + reader.Value + "\r\n";
// Encontra o link
reader.ReadToFollowing("link");
// Lê o conteúdo do link
reader.Read();
// Exibe o valor do link
TextBoxResultado.Text += reader.Value + "\r\n\r\n";
}
}
Comentários