Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Saving Binary Data to a Zip file from the XMLHTTP.ResponseBody

Posted on 2004-08-03
16
Medium Priority
?
468 Views
Last Modified: 2008-03-17
I have a windows application that requests a file from our Broadvision XML server. The File is compressed in a zip format and is returned in the XMLHTTP.ResponseBody. How can I get this saved into a new zip file on my client pc. I have not been able to find a method that will stream the ResponseBody into a file. This is a c# application.
0
Comment
Question by:stechsrw
[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
  • 10
  • 6
16 Comments
 
LVL 12

Expert Comment

by:vascov
ID: 11705001
If you're using C#, then i would suggest making use of
System.Net.WebClient instead.

It has a very convenient method named DownloadFile.

WebClient wc = new WebClient();
wc.DownloadFile( someUrl, someLocalPath );

hth

Vasco
0
 

Author Comment

by:stechsrw
ID: 11705041
I cannot use the WebClient because the server runs BroadVision XML. I need to send an XML request to the server first which creates the response XML, including the file I need and returns it in the responsebody.
0
 

Author Comment

by:stechsrw
ID: 11705416
Here is the latest thing that I have tried, but I cannot get the .Open to work since it requires 4 parameters in .Net. I don't kow what the first should be, what is the source that it wants?

ADODB.Stream s = new ADODB.Stream();

s.Open(?????object source ???????, ADODB.ConnectModeEnum.adModeWrite, ADODB.StreamOpenOptionsEnum.adOpenStreamAsync, "", "");

s.Write(XMLHttp.ResponseBody);
s.SaveToFile("Download.zip", ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
s.Close();

I have also tried using a StreamReader and a BinaryReader, but I get type mismatches trying to pass it the ResponseBody. The type on the ResponseBody is Byte().
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:vascov
ID: 11705525
Hi stechsrw,

I'll try to come up with the equivalent in .NET using HttpWebRequest.

On any case, you do have another method of the interface IXmlHttpRequest, called ResponseStream, which is probably more close to what you want.
Try passing your object as the first parameter (xmlhttp).

Also, another possible way would be to look at the interface UCOMIStream.

hth

Vasco
0
 

Author Comment

by:stechsrw
ID: 11705642
I have tried passing my object as the first parameter and I still get an error that the the parameters are no good. I have also tried to use the ResponseStream, but it would not allow me to set andthing equal to it. What should I define and set = ResponseStream? Can you give me an example of getting the stream out and then saving it to a file?
0
 
LVL 12

Accepted Solution

by:
vascov earned 1500 total points
ID: 11706314
Hi stechsrw,

Here's some code you can try out.

            public const int BUFFER_SIZE = 1024 * 4;

            public void SendXmlAndSaveBinary( XmlDocument Doc, string Url, string Path )
            {
                  BinaryWriter      bw = null;

                  try
                  {
                        WebRequest wr = HttpWebRequest.Create( Url );
                        wr.Method = "POST";
                        wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

                        XmlWriter w = new XmlTextWriter( wr.GetRequestStream(), System.Text.Encoding.UTF8 );
                        Doc.WriteTo( w );
                        w.Close();

                        WebResponse r = wr.GetResponse();
                        Stream sr = r.GetResponseStream();

                        bw = new BinaryWriter( new FileStream( Path, FileMode.Create ) );

                        byte[] buf = new byte[ BUFFER_SIZE ];

                        int c = sr.Read( buf, 0, BUFFER_SIZE );
                        while( c > 0)
                        {
                              bw.Write( buf, 0, c );
                              c = sr.Read( buf, 0, BUFFER_SIZE );
                        }
                  }
                  finally
                  {
                        if ( null != bw )
                              bw.Close();
                  }
            }

Usage like:
                  SendXmlAndSaveBinary( somXmlDoc, someUrl, somePath );


Vasco
0
 

Author Comment

by:stechsrw
ID: 11707231
Let me start by saying thank you very much for your help so far!

I tried your code and get an error on the line:
XmlWriter w = new XmlTextWriter( wr.GetRequestStream(), System.Text.Encoding.UTF8 );

The error: + The underlying connection was closed: The remote name could not be resolved.
0
 
LVL 12

Expert Comment

by:vascov
ID: 11708391
That is probably a problem with the Url you're using.

Are you sure you're able to communicate with it ?
Also, note that i've used Integrated Security...

Can you share the Url you're using ?

Vasco
0
 

Author Comment

by:stechsrw
ID: 11708483
I got it working by calling

wr.GetRequestStream();
then calling
XmlWriter w = new XmlTextWriter( wr.GetRequestStream(), System.Text.Encoding.UTF8 );

I am not able to get a response back and save it out to a file. Thank you for your help.
0
 

Author Comment

by:stechsrw
ID: 11708508
I mean I AM able to get a response back, but it says that my xml being sent is no good. However when I send it using the XMLHttp object it is ok. I will continue looking into this. Thanks again for your help!!
0
 
LVL 12

Expert Comment

by:vascov
ID: 11708530
Can you show me your code ?

The code before calling into the method ?

thx

Vasco
0
 

Author Comment

by:stechsrw
ID: 11708594
                 string FileQuery = null;
                  FileQuery = "<GET_CUSTINFO_002>";
                        FileQuery += "<CNTROLAREA>";
                              FileQuery += "<BSR>";
                                    FileQuery += "<VERB>GET</VERB>";
                                    FileQuery += "<NOUN>CUSTINFO</NOUN>";
                                    FileQuery += "<REVISION>002</REVISION>";
                              FileQuery += "</BSR>";
                              FileQuery += "<SENDER>";
                                    if (form.cbZipped.Checked == true)
                                    {
                                          FileQuery += "<LOGICALID>SMARTCART PRO ZIP</LOGICALID>";
                                    }
                                    else
                                    {
                                          FileQuery += "<LOGICALID>SMARTCART PRO</LOGICALID>";
                                    }
                                    FileQuery += "<COMPONENT>SALES FORCE AUTOMATION</COMPONENT>";
                                    FileQuery += "<TASK>DOWNLOAD</TASK>";
                                    FileQuery += "<CONFIRMATION>2</CONFIRMATION>";
                                    FileQuery += "<LANGUAGE>ENG</LANGUAGE>";
                              FileQuery += "</SENDER>";
                              FileQuery += "<DATETIME>";
                                    FileQuery += "<YEAR>" + DateTime.Now.Year + "</YEAR>";
                                    FileQuery += "<MONTH>" + DateTime.Now.Month + "</MONTH>";
                                    FileQuery += "<DAY>" + DateTime.Now.Day + "</DAY>";
                                    FileQuery += "<HOUR>" + DateTime.Now.Hour + "</HOUR>";
                                    FileQuery += "<MINUTE>" + DateTime.Now.Minute + "</MINUTE>";
                                    FileQuery += "<SECOND>" + DateTime.Now.Second + "</SECOND>";
                              FileQuery += "</DATETIME>";
                        FileQuery += "</CNTROLAREA>";
                        FileQuery += "<DATAAREA>";
                              FileQuery += "<GET_CUSTINFO>";      
                                    FileQuery += "<CIHEADER>";
                                          FileQuery += "<SALESINFO>";
                                          FileQuery += "<SALESPERSN>Test</SALESPERSN>";
                                                FileQuery += "<USERAREA>";
                                                      FileQuery += "<BROADVISION>";
                                                            FileQuery += "<BROADVISION.USERNAME>" + username + "</BROADVISION.USERNAME>";
                                                            FileQuery += "<BROADVISION.USERPASSWD>"+ password + "</BROADVISION.USERPASSWD>";
                                                      FileQuery += "</BROADVISION>";
                                                FileQuery += "</USERAREA>";
                                          FileQuery += "</SALESINFO>";
                                    FileQuery += "</CIHEADER>";
                                    FileQuery += "<SYNC_ECATALOG>";
                                          FileQuery += "<CATHEADER>";
                                                FileQuery += "<RSSTART>" + rsstart + "</RSSTART>";
                                                FileQuery += "<MAXITEMS>1000</MAXITEMS>";
                                          FileQuery += "</CATHEADER>";
                                    FileQuery += "</SYNC_ECATALOG>";
                              FileQuery += "</GET_CUSTINFO>";
                        FileQuery += "</DATAAREA>";
                  FileQuery += "</GET_CUSTINFO_002>";
                  
                  form.lblStatus.Text = "Saving XML Request";
                  form.lblStatus.Refresh();

                  File.Delete("DownloadRequest.xml");
                  XmlDocument xmldocRequest = new XmlDocument();
                  xmldocRequest.LoadXml(FileQuery);
                  xmldocRequest.Save("DownloadRequest.xml");

*Then call the function.
0
 

Author Comment

by:stechsrw
ID: 11708700
I tried writing the content of the Doc to a file as well and the Doc.WriteTo(w) will not put anything in the file. I used w to = XmlWriter w = new XmlTextWriter("file.xml", System.Text.Encoding.UTF8 );
0
 
LVL 12

Expert Comment

by:vascov
ID: 11708707
What parameters do you pass into the function ? i'm actually interested in the function call.
Also, could you send me the variable FileQuery after being constructed ?

Please note that the characters the user puts in the password and username have to be Xml encoding "friendly", otherwise the xml wont parse.

I would also strongly advise you to use StringBuilder instead of concatenating all those strings. Whenever you concatenate a string you're actually building a new one from scratch, copying the contents of the old one + the new one... If you use stringbuilder, it minimizes the "resizes" dramatically.

Vasco
0
 

Author Comment

by:stechsrw
ID: 11708769
using Doc.WriteContent I can at least get data out of my xml file. I am looking into what the data looks like on the server side.
0
 

Author Comment

by:stechsrw
ID: 11708790
I just saw your response. I will try that.  I can't give out the username or password, but they are plain text, no special chars.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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