Geçenlerde bir soru üzerine araştırırken yaptığım örnek. Javascript ve saop ile web servisini server-side yapı olmadan Html sayfasında göstermek.
<html>
<head>
<title>Javascript ile Web Servis Tüketmek</title>
<script>
var xmlhttp;
function get()
{
var city = document.getElementById('city').value;
xmlhttp=N';
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc)
{
xmlhttp=N';
}
}
if(!xmlhttp&&typeof XMLHttpRequest!="undefined")
{
xmlhttp=new XMLHttpRequest();
}
if (xmlhttp!=N')
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("POST", "http://www.webservicex.net/WeatherForecast.asmx?op=GetWeatherByPlaceName", true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://www.webservicex.net/GetWeatherByPlaceName");
var text = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>';
text += '<GetWeatherByPlaceName xmlns="http://www.webservicex.net"><PlaceName>' + city + '</PlaceName></GetWeatherByPlaceName>';
text += '</soap:Body></soap:Envelope>';
xmlhttp.send(text);
}
else
{
document.getElementById('dvResult').innerHTML ="Your browser does not support XMLHTTP.";
}
}
function state_Change()
{
document.getElementById("picture").style.display = 'inline';
if (xmlhttp.readyState == 4)
{
document.getElementById('status').innerHTML = 'OK!';
document.getElementById("picture").style.display = 'none';
var nodes = xmlhttp.responseXML.getElementsByTagName("WeatherData");
var htmlR = '';
htmlR = '<table border=1>';
htmlR += '<tr>';
htmlR += '<th>Day</th><th>WeatherImage</th><th>MaxTemperatureC</th><th>MinTemperatureC</th>';
htmlR += '</tr>';
for(var i = 0;i<nodes.length;i++)
{
try
{
htmlR += '<tr>';
htmlR += '<td>' + nodes[i].childNodes.item(0).text + '</td>';
htmlR += '<td><img src=' + nodes[i].childNodes.item(1).text + ' /></td>';
htmlR += '<td>' + nodes[i].childNodes.item(4).text + '</td>';
htmlR += '<td>' + nodes[i].childNodes.item(5).text + '</td>';
htmlR += '</tr>';
}
catch(e)
{
;
}
}
htmlR += '</table>';
document.getElementById('dvResult').innerHTML = htmlR;
}
}
</script>
</head>
<body>
<b>Şehir :</b>
<input type="text" id="city" value="alabama" />
<input type="button" value="Hava Durumu" onclick="get()" />
<br />
<div id="dvResult">
</div>
<img alt="wait" src="circle.gif" id="picture" style="display: none;" />
<span id="status"></span>
<br />
</body>
</html>
İkinci basit Web Part'ımız.
string getWeatherFromChache(string Sehir)
{
if (HttpContext.Current.Cache[Sehir] == N')
{
try
{
string servis = "http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=" + Sehir + "&CountryName=turkey";
WebClient client = new WebClient();
string data = client.DownloadString(servis);
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
string nodes = doc.GetElementsByTagName("string")[0].InnerText;
doc.LoadXml(nodes);
string Temperature = doc.SelectSingleNode("/CurrentWeather/Temperature").InnerText;
string RelativeHumidity = doc.SelectSingleNode("/CurrentWeather/RelativeHumidity").InnerText;
StringBuilder sb = new StringBuilder();
sb.Append("<table style=font-family:verdana;font-size:12px; border=0 width=100%><tr>");
sb.Append("<td width=20%><b>Sıcaklık : </b></td>");
sb.Append("<td width=80%>");
sb.Append(Temperature);
sb.Append("</td></tr>");
sb.Append("<td><b>Nem : </b></td>");
sb.Append("<td>");
sb.Append(RelativeHumidity);
sb.Append("</td></tr>");
sb.Append("</table>");
HttpContext.Current.Cache.Insert(Sehir, sb.ToString(), N', DateTime.Now.AddHours(5), TimeSpan.Zero);
}
catch
{
HttpContext.Current.Cache.Insert(Sehir, "Bilgi alınamıyor.", N', DateTime.Now.AddSeconds(5), TimeSpan.Zero);
}
}
return HttpContext.Current.Cache[Sehir].ToString();
}
Şehir Listesi:
<?xml version="1.0" encoding="utf-8" ?>
<Sehirler>
<Sehir>Istanbul / Ataturk</Sehir>
<Sehir>Ankara / Esenboga</Sehir>
<Sehir>Ankara / Etimesgut</Sehir>
<Sehir>Murted Tur-Afb</Sehir>
<Sehir>Adana / Sakirpasa</Sehir>
<Sehir>Adana / Incirlik</Sehir>
<Sehir>Afyon</Sehir>
<Sehir>Antalya</Sehir>
<Sehir>Gaziantep</Sehir>
<Sehir>Iskenderun</Sehir>
<Sehir>Konya</Sehir>
<Sehir>Merzifon</Sehir>
<Sehir>Samsun</Sehir>
<Sehir>Sivas</Sehir>
<Sehir>Zonguldak</Sehir>
<Sehir>Malatya / Erhac</Sehir>
<Sehir>Kayseri / Erkilet</Sehir>
<Sehir>Sivrihisar</Sehir>
<Sehir>Tokat</Sehir>
<Sehir>Aydin</Sehir>
<Sehir>Bursa</Sehir>
<Sehir>Balikesir</Sehir>
<Sehir>Bandirma</Sehir>
<Sehir>Canakkale</Sehir>
<Sehir>Eskisehir</Sehir>
<Sehir>Izmir / Adnan Menderes</Sehir>
<Sehir>Isparta</Sehir>
<Sehir>Usak</Sehir>
<Sehir>Dalaman</Sehir>
<Sehir>Akhisar</Sehir>
<Sehir>Corlu</Sehir>
<Sehir>Bodrum</Sehir>
<Sehir>Elazig</Sehir>
<Sehir>Diyarbakir</Sehir>
<Sehir>Erzincan</Sehir>
<Sehir>Erzurum</Sehir>
<Sehir>Kars</Sehir>
<Sehir>Trabzon</Sehir>
<Sehir>Urfa</Sehir>
<Sehir>Van</Sehir>
<Sehir>Batman</Sehir>
</Sehirler>