Link to home
Start Free TrialLog in
Avatar of stechsrw
stechsrw

asked on

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.
Avatar of vascov
vascov
Flag of United States of America image

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
Avatar of stechsrw
stechsrw

ASKER

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.
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().
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
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?
ASKER CERTIFIED SOLUTION
Avatar of vascov
vascov
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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.
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!!
Can you show me your code ?

The code before calling into the method ?

thx

Vasco
                 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.
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 );
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
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.
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.