Download xml file from webservices

By Emanuele Bartolesi at March 20, 2009 04:39
Filed Under: Asp.net, C#, WebServices

Sometimes, some network configurations don't allow to access directly to web services.
One way is download the xml file that xml web services returns and load that in a dataset.
I wrote a small function to download xml file from web services.
It's simple, but it's a good starting point to develop other things.

 

public bool DownloadXmlFromService()
{
string result = "";
try
{
WebProxy proxy = new WebProxy(" proxy address ", port number );
proxy.Credentials = new NetworkCredential( user id , password, domain );
WebRequest request = WebRequest.Create("http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=Italy");
request.Proxy = proxy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
char [] chars = new Char[256];
int count = reader.Read(chars, 0, 256);
while(count > 0)
{
string str = new String(chars, 0, 256);
result = result + str;
count = reader.Read(chars, 0, 256);
}
response.Close();
stream.Close();
reader.Close();

result = result.Replace("&lt;","<");
result = result.Replace("&gt;",">");

if (File.Exists("temp.xml"))
{
File.Delete("temp.xml");
}

System.IO.StreamWriter ToFile = new StreamWriter("temp.xml");
ToFile.Write(result);
ToFile.Close();
}
catch(Exception exp)
{
string str = exp.Message;
return false;
}

return true;
}

About me

I will also give you some useful tips, based on the modest wisdom gained during the years that I've worked as a developer and project manager.

Widget

Ohloh profile for Emanuele Bartolesi

 

Wakoopa

Software tracking

 

Software tracking

from Amazon



hacker emblem



Scarica il pdf di Game
Rivista Game di videogiochi
Trucchi videogiochi