Monthly Archives: June 2010
Call a WebService from VBScript
While I was writing some code on VBScript to avoid manual tasks that we currently do with a bunch of files, I needed to automatically upload some information extracted from all these files to a SQL database. One way to do it is using a Web Service that receives the data and store it into the database.
Our Web Service is able to receive the following protocols: SOAP 1.1, SOAP 1.2 and HTTP POST. For simplicity, we are going to use HTTP POST, which receive the parameters in the query string format (param1=value1¶m2=value2&…). In the other protocols (SOAP) we would need to send the parameters in a XML way (which is more powerful but a little more extensive to implement).
The expected HTTP request will be:
POST /WebService.asmx/WebMethod HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: length param=string
And the response (in XML format):
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">string</string>
So, to call the Web Service directly from the VBScript, we can use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 'The object that will make the call to the WS Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP") 'The object that will receive the answer from the WS Set oXMLDoc = CreateObject("Microsoft.XMLDOM") strParam = "string to pass" 'Tell the name of the subroutine that will handle the response oXMLHTTP.onreadystatechange = getRef("HandleStateChange") 'Initializes the request (the last parameter, False in this case, tells if the call is asynchronous or not oXMLHTTP.open "POST", "http://localhost/WebService.asmx/WebMethod", False 'This is the content type that is expected by the WS using the HTTP POST protocol oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 'Now we send the request to the WS oXMLHTTP.send "parameter=" & strParam Sub HandleStateChange() Dim szResponse 'When the call has been completed (ready state 4) If oXMLHTTP.readyState = 4 Then szResponse = oXMLHTTP.responseText oXMLDoc.loadXML szResponse 'If the WS response is not in XML format, there is a problem If oXMLDoc.parseError.errorCode <> 0 Then WScript.Echo "ERROR:" WScript.Echo oXMLHTTP.responseText WScript.Echo oXMLDoc.parseError.reason Else WScript.Echo "Result: " & oXMLDoc.getElementsByTagName("string")(0).childNodes(0).Text End If End If End Sub |
The ready state tells us about the connection with the Web Service:
- 0: Uninitialized – open() has not been called yet.
- 1: Loading – send() has not been called yet.
- 2: Loaded – send() has been called, headers and status are available.
- 3: Interactive – Downloading, responseText holds the partial data.
- 4: Completed – Finished with all operations.
Convert a String to a Stream
In C#, to convert a String into a Stream object, we need to use the GetBytes method (from the Encoding.ASCII package), this way:
byte[] bytes = Encoding.ASCII.GetBytes(xmlContent); |
And then, use that byte array when instantiate a Stream (for example, MemoryStream or FileStream):
MemoryStream stream = new MemoryStream(bytes); |
Windows 7 bug affecting Subversion
There is a bug on Windows 7 (and Windows Server 2008 R2) that relates to corrupted files error messages and affects several SVN’s operations (like commit and update). The detailed error message you can get is something similar to:
svn: Can't move '[repo]\.svn\tmp\entries' to '[repo]\.svn\entries': The file or directory is corrupted and unreadable.
And you can even get a Windows error message telling you about a corrupted file.
As I found in here and here, there is nothing to be afraid of, it is just another bug/error on Windows that is going to be fixed on SP1 (but there is already a hotfix ready to install). I haven’t finished test it myself (just missing the restart after the hotfix installation).
The hotfix’s download requires you to give your email address to Microsoft before they send you the link where you can download it (some way annoying). I have uploaded on my own server to avoid the process if I need it again, so you can get it from here if you want (remember that you should NOT download executables from non-trusted sources as they can contain viruses and harm your computer).