IUAATech
asked on
calling web service with XML output
I need to make 1000+ calls to the following web service and download the XML output that results from each call - http://comtrade.un.org/ws/
I am fairly new to web services, so please bear with me.
I wrote some C# code with which I can call the web service - see attached.
Now, I need to call the web service with a couple of parameters - http://comtrade.un.org/ws/get.aspx?cc=TOTAL&px=S3&r=372&y=2006&p=4,%208,%2024&rg=1,2&so=9999
I will need to loop through various values for "cc" and "r".
The web service returns a XML file which I plan to import into Excel.
Can someone please help me with the code that will help me download the files?
thanks.
I am fairly new to web services, so please bear with me.
I wrote some C# code with which I can call the web service - see attached.
Now, I need to call the web service with a couple of parameters - http://comtrade.un.org/ws/get.aspx?cc=TOTAL&px=S3&r=372&y=2006&p=4,%208,%2024&rg=1,2&so=9999
I will need to loop through various values for "cc" and "r".
The web service returns a XML file which I plan to import into Excel.
Can someone please help me with the code that will help me download the files?
thanks.
namespace comtradedata
{
class Program
{
static void Main(string[] args)
{
StringBuilder sbSource;
// Create the web request
HttpWebRequest request
= WebRequest.Create("http://comtrade.un.org/ws/") as HttpWebRequest;
request.Proxy = WebProxy.GetDefaultProxy();
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Read it into a StringBuilder
sbSource = new StringBuilder(reader.ReadToEnd());
// Console application output
Console.WriteLine(sbSource.ToString());
Console.ReadLine();
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think,
You should use XML classes available in .NET (like XMLDocument, XMLTextReader) to parse the response stream in XML and then you can convert your XML in Excel.
you should also check the HttpResponse's StatusCode property to know if the response status (i.e. check for 200)
You should use XML classes available in .NET (like XMLDocument, XMLTextReader) to parse the response stream in XML and then you can convert your XML in Excel.
you should also check the HttpResponse's StatusCode property to know if the response status (i.e. check for 200)
Forget to add example code of XmlTextReader:
using System.Xml;
// Retrieve XML document
XmlTextReader reader = new XmlTextReader("Your WEB-SERVICE URL");
// Skip non-significant whitespace
reader.WhitespaceHandling = WhitespaceHandling.Significant;
// Read nodes one at a time
while (reader.Read())
{
// Print out info on node
//do some operations.
}
ASKER
I am trying mayank_joshi's approach.
Which namespace should I use for "Response" and HttpCacheability? I already have a reference to System.Web namespace in my code.
Which namespace should I use for "Response" and HttpCacheability? I already have a reference to System.Web namespace in my code.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks gery128 - yeah, I am targeting a console app and I got it to work. thanks.
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-web-services