• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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

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
stechsrw
Asked:
stechsrw
  • 10
  • 6
1 Solution
 
vascovCommented:
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
 
stechsrwAuthor Commented:
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
 
stechsrwAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
vascovCommented:
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
 
stechsrwAuthor Commented:
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
 
vascovCommented:
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
 
stechsrwAuthor Commented:
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
 
vascovCommented:
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
 
stechsrwAuthor Commented:
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
 
stechsrwAuthor Commented:
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
 
vascovCommented:
Can you show me your code ?

The code before calling into the method ?

thx

Vasco
0
 
stechsrwAuthor Commented:
                 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
 
stechsrwAuthor Commented:
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
 
vascovCommented:
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
 
stechsrwAuthor Commented:
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
 
stechsrwAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now