Solved

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

Posted on 2004-08-03
16
459 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
Technology Partners: 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 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 500 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

752 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