Pierre Greborio.NET

Talking about .NET world

DIME message with VBScript

To implement a client and a Web Services with .NET that uses attachements is pretty simple. WSE does a lot of stuff for us. But, if we want consume a web service with attachement from a VB or VBScript client we have to write more code.

First of all we have to download Microsoft SOAP Toolkit 3.0 which provides an object model for DIME also. Then we should have a WebMethod with attachements, such as the following simple sample:

[WebMethod]
public string GetLastImage()
{
  DimeAttachment attachment = new DimeAttachment("image/jpeg",
     TypeFormatEnum.MediaType, Server.MapPath(@"\images\Test.jpg"));
  HttpSoapContext.ResponseContext.Attachments.Add(attachment);
  return "Test.jpg";
}

On the client side we can then create a VBS file as following:

WScript.Echo ("-----Start------")
' Connect to the web service
EndPointURL = "
http://webstudy/WebServices/DimeSample.asmx";
Set connector = CreateObject("MSOSOAP.HttpConnector30")
connector.Property("EndPointURL") = EndPointURL
connector.Connect

' Define SOAP message
connector.Property("SoapAction") = "urn:pierregreborio-it/GetLastImage"
connector.BeginMessage

Set Serializer = CreateObject("MSOSOAP.SoapSerializer30")
Serializer.Init connector.InputStream

Serializer.StartEnvelope
Serializer.StartBody
Serializer.StartElement "GetLastImage", "urn:pierregreborio-it"
Serializer.EndElement
Serializer.EndBody
Serializer.EndEnvelope

connector.EndMessage

' Post the message and get the response with the DIME parser (this is a DIME message)
Set Reader = CreateObject("MSOSOAP.SoapReader30")
Set Parser = CreateObject("MSOSOAP.DimeParser30")
Reader.LoadWithParser Connector.OutputStream, Parser

If Not Reader.Fault Is Nothing Then
  WScript.Echo(Reader.FaultString.Text)
Else
  ' Get the image from the attachments 
  Set ImageAttach = Reader.Attachments.Item(0)
  FileName = "ReceivedImage.jpg"
  Set fso = CreateObject("Scripting.FileSystemObject")
  If (fso.FileExists(FileName)) Then
     fso.DeleteFile FileName
  End If
  ImageAttach.SaveToFile FileName
End If

WScript.Echo("-----Done------")

This sample doesn't contains all error controls but gives an idea how to get an attachment with SOAP Toolkit 3.0...hopefully.

Posted: Jul 09 2003, 05:36 PM by PierreG | with 5 comment(s)
Filed under:

Comments

Christian Weyer said:

Good stuff, Pierre. Why not supply a small ZIP archive with a sample app people can play with?
Just a thought ...
# July 9, 2003 6:02 AM

Pierre Greborio said:

That's a good idea. Will be available soon. Thanks.

# July 14, 2003 4:14 AM

Guillaume MEYER said:

Excellent for pictures...
But how to send files of unknown format as binary ?
Which FileFormat can I use for a DimeAttachment as I don't know the file type... ?
# November 10, 2003 7:51 AM

Young Lee said:

Very nice !!! Thank you.
# May 17, 2004 3:20 PM

Jerry said:

Nice work. Simple and highly informational.

Thanks,
Jerry
# July 23, 2004 1:47 PM