Solved

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

Posted on 2004-08-03
16
421 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now