Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling web service with XML output

Posted on 2011-03-06
7
Medium Priority
?
678 Views
Last Modified: 2012-08-14
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.
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();
            } 
        }
    }
}

Open in new window

0
Comment
Question by:IUAATech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 1600 total points
ID: 35055064
add the following code after:-
 // Read it into a StringBuilder  
 sbSource = new StringBuilder(reader.ReadToEnd());


 
 Response.Clear();
  Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  Response.Charset = "";
 
  // If you want the option to open the Excel file without saving then
  // comment out the line below
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.ContentType = "application/vnd.xls"; 
  Response.Write(sbSource.ToString());
  Response.End();

Open in new window

0
 
LVL 9

Expert Comment

by:gery128
ID: 35057545
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)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:gery128
ID: 35057580
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.
}  

Open in new window

0
 

Author Comment

by:IUAATech
ID: 35058520
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.
0
 
LVL 9

Assisted Solution

by:gery128
gery128 earned 400 total points
ID: 35065713
HttpCacheability is an enum which comes under System.Web. So you should get it if you are importing that namespace. Try full namespace name, System.Web.HttpCacheability.
Also for Response, you may be trying to use Response in demo console application you have created. Response is the property of the System.Web.UI.Page class, If you try it in ASP.NET application it will work. Response is not available in a Console Application as there is no HttpContext.
You may try WebClient class to generate request and get response in console application, but you will get response string only you won't be able to generate Excel file using System.Web.UI.Page.Response.
Check out this link:
WebClient in Console Application
Which type of application are you targeting? ASP.NET or Console Application (executable) ? If Console Application then, you need to change your approach related to Response.
0
 

Author Comment

by:IUAATech
ID: 35069415
thanks gery128 - yeah, I am targeting a console app and I got it to work. thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question