Using WinInet to call a Web service
I am working on a MFC/C++ application, and need to call a Web service. I would like to avoid adding any dependency or use any other API (MSXML or SOAP Toolkit), and continue with my application's minimum requirements (which includes Internet Explorer 5.5 or above), and still write the Web service client code. In other words, can you please show me an example of using WinInet to call a Web service? |
Answer:Click here to download a sample console application that uses MFC WinInet classes to call a Web service. MFC provides wrapper classes around WinInet API. These classes simplify the task of writing HTTP/FTP client applications. Following are the eight steps required to send a HTTP request using MFC WinInet classes:
Click here to download
the above sample console application.
Related Article: MSXML on a clean machine |
DOM and limiting the size of XML document
I am using MSXML DOM to load and process an XML document (posted to my C++ ISAPI DLL code). I want to make sure that the load succeeds only if the XML document is less than or equal to some pre-specified size. Is it possible to achieve this using MSXML? |
Answer:Click here to download a sample console application associates with this answer. The good news is that the recent MSXML 3.0 SP4 and MSXML 4.0 SP2 includes a new property called MaxXMLSize that you can set to make sure that DOM does not load the document if it exceeds the specified size. Note that this size is not the file size, but it refers to the size of document as loaded in the memory - and that the XML encoding and entities (if any) have effect on the document size loaded in the memory.
The above code uses MSXML 4.0 SP2 and sets the MaxXMLSize to 1 (KB). If the specified XML document (c:\1.xml in this case) exceeds the specified size (1KB), the load operation would fail/abort, else if the document is well-formed, the load will succeed.
Click here to download a sample console application associates with this answer. |
Binary data from Visual Basic 6.0 to ASP.NET Web service
In my Visual Basic 6.0 client application, I would like to make a call to an ASP.NET Web service method, and pass it some binary data. Can you please show some sample code that illustrates this? |
Answer:Click here to download a ZIP file that contains sample ASP.NET Web service and two Visual Basic 6.0 client applications (one uses MSXML XMLHTTP to send binary data to the Web service, and the other uses Microsoft SOAP Toolkit 3.0). From the Visual Basic 6.0 client code, you can use MSXML XMLHTTP or the Microsoft SOAP Toolkit to invoke a Web service method. Before we look at the Visual Basic client code, let's first study the ASP.NET Web service code. This sample ASP.NET Web service has only one method called, SaveImage. This Web method accepts two parameters – the filename and the binary data. All it does is saves the input binary data into the disk file with the specified filename. ASP.NET Web method
The above Web method code creates a file under the
c:\temp\empImages folder and writes the input byte array data into this file and closes the stream. Make sure correct permissions are set for this code to execute (for instance, if anonymous access is enabled on this Web site/virtual folder, make sure the anonymous account has write access to this directory).
Note that even though the Web method is called SaveImage, it's not like you can pass only image files with this sample; this sample should work with any kind of file (text/binary such as PDFs, Doc files, etc.). Here is how you would call this Web service method from a Visual Basic 6.0 application using Microsoft SOAP toolkit 3.0:
Remember to add reference (Project | References) to Microsoft SOAP Toolkit type library.
The above Visual Basic code creates an instances of SoapClient30 class,
accepts the file name (using the text box control on the form), extracts the file name (excludes path),
reads the file into a byte array and finally calls the SaveImage Web service method.
In this case, the SOAP Toolkit converts byte array (binary data) into text using Base64 encoding, and then sends that as the second parameter. On the Web method side, the ASP.NET automagically converts the Base64 encoded text into binary data or byte [].
The above screenshot shows the MSSoapT (the SOAP trace tool that comes with Microsoft SOAP Toolkit), which captures the Web service method call. Note that the imageData parameter is a Base64 encoded text string. Let's say you do not want to add dependency on SOAP toolkit (that is, avoid installing it on the client machines), you can use MSXML (version 3.0 ships with Internet Explorer 6.0) to call a Web service and pass binary data. The following Visual Basic 6.0 code uses MSXML 3.0 to first convert the binary data into Base64 and then pass it to a Web service using XMLHTTP. Using MSXML 3.0 XMLHTTP
Remember to add reference to MSXML 3.0 type library.
The above Visual Basic 6.0 code defines a string variable and initializes it to the SOAP request envelope.
It then extracts the file name (from the file name with full path as specified for the text box on the form).
Next, it reads the file content into a byte array, sets the imageData
node dataType to bin.base64.
Now, when byte array is assigned to this node, MSXML automagically converts it into
base64 encoded string. The code then uses MSXML XMLHTTP to POST the SOAP request envelope
XML to the Web service.
Click here to download a ZIP file that contains sample ASP.NET Web service and two Visual Basic 6.0 client applications (one uses MSXML XMLHTTP to send binary data to the Web service, and the other uses Microsoft SOAP Toolkit 3.0). Related Links: |