I faced the following problem:
I need to parse many cookies in vbscript and this is my code so far:
Dim url : url = "https://MYURL"
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
http.open "GET", url, False
http.Send
Dim aRes : aRes = Split(http.getAllResponseHeaders(), vbCrLf)
Dim i
For i = 0 To UBound(aRes)
aRes(i) = Mid(aRes(i), Len(header) + 1, Len(aRes(i)))
Next
Set http = Nothing
Dim cookie
For Each cookie In aRes
TextBoxTestOutput.Text = TextBoxTestOutput.Text + VBNewline + cookie
Next
This just returns the content of one cookie, but there are two and the one that's been shown has "/something" path while the other one has "/".
I think this is a IE related Problem. I am using IE10.
What could be the cause for not getting the other cookie?
The script receives cookies via http-GET. That's why it does not get a previously set cookie which is not sent in the answer of this GET-request.
Related
I am new to VBscript and am looking for some help to do a POST to an API and pass it a JSON string containing id, password and a scope, then get an answer and parse it. Here is the call I need to make:
POST https://integrations.ezyvet.com/call/getAccessToken { "partner_id": "id8888", "client_id": "id12345", "client_secret": "secret12345", "grant_type": "client_credentials", "scope": “read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem" }
Here is my code:
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("c:\temp\JSONoutput.txt", True)
set json = CreateObject("Chilkat_9_5_0.JsonObject")
jsonStr = "{""partner_id"": ""id8888"", ""client_id"": ""id12345"", ""client_secret"": ""secret12345"", ""grant_type"": ""client_credentials"", ""scope"": ""read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem""}"
success = json.Load(jsonStr)
If (success <> 1) Then
outFile.WriteLine(json.LastErrorText)
WScript.Quit
End If
set http = CreateObject("Chilkat_9_5_0.Http")
I need to make my POST here and get a response and am not sure how. Please help.
Thanks a million.
Hi and welcome to stack overflow!
You have tagged chilkat in your question, however you haven addressed it anywhere in the body or the tittle of it, so I was not sure if answer pointing this or not, so I will try to make a bit of both.
without chilkat
You can do this in pure vbs by using ajax, the short answer would be
Dim request
Set request = CreateObject("MSXML2.XMLHTTP")
request.open "GET", "http://www.example.com", False '(1)
request.send infoToSend '(2)
'(3)
Here you set eather "POST" or "GET"
infoToSend contains the information data, formatted as "key=value&key2..."
request.responseText here contains the servers answer as text, parse it as json if you need
You can find information here.
with chilkat
If you still want to use chilkat the main documentation of the http object is here, here is everything you need. If you need an example tho here I've found two:
making a request: https://www.example-code.com/vbscript/http_xmlHttpRequestVerbs.asp
sending a json: https://www.example-code.com/vbscript/http_put_json.asp
I wont paste it here because its too long but the core part of your interest is that you is:
set request = CreateObject("Chilkat_9_5_0.HttpRequest") '(1)
request.HttpVerb = "PUT" '(2)
success = request.LoadBodyFromString(xmlStr,"utf-8") '(3)
Set response = http.SynchronousRequest(endpointDomain,endpointPort,endpointSsl,request)' (4)
you have to create a httpRequest
you set here eather get or post
3.you load here your content, it is your json or what you will send but formatted appropiately
here you have response contain a HttpResponse object with the result
documentation on the HttpResponse, and the HttpRequest
I have a classic asp file that needs to connect to a web service.
I am confused as to how to define the URL for the web service and the Namespace in the SOAPAction.
When I run my code and write a Response.Write for the return value of the method that I am calling in the web service, it either returns the wsdl or the web page for the service
This code displays the web service html as if you are entering the web service .svc url:
Dim strSoapReq
strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
strSoapReq = strSoapReq & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
strSoapReq = strSoapReq & "<s:Body>"
strSoapReq = strSoapReq & "<TestMethod xmlns=""http:<serverName:<port>/PagingService/PagingService"">"
strSoapReq = strSoapReq & "</TestMethod>"
strSoapReq = strSoapReq & "</s:Body>"
strSoapReq = strSoapReq & "</s:Envelope>"
'Create server-side component to make requests
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http:<serverName:<port>/PagingService/PagingService.Paging.svc"
httpRequest.Open "GET", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", URL & "/TestMethod"
httpRequest.Send(strSoapReq)
Dim strResult
strResult = httpRequest.responseText
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
If I add the ?wsdl to the end of the service url, it shows the WSDL.
How can I call the method in the web service?
UPDATE
I changed my code to the following:
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "http://<server>/PagingService/"
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http://<server>/PagingService/PagingService.Paging.svc?WSDL"
httpRequest.Open "POST", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", "http://<server>/PagingService/TestMethod"
' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param
' Creates an XML DOM object.
Set DOM = CreateObject("MSXML2.DOMDocument.6.0")
' Creates the main elements.
Set Envelope = DOM.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
DOM.appendChild Envelope
Set Body = DOM.createElement("soap:Body")
Envelope.appendChild Body
' Creates an element for the TestMethod function.
Set Operation = DOM.createNode(1, "TestMethod", NS)
Body.appendChild Operation
' Releases the objects.
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing
httpRequest.Send(DOM.xml)
Dim strResult
strResult = httpRequest.status
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
The http.Status result with this code is: 415 - unsupported media
I saw a post with this error and they corrected it by changing the Content-Type. When I tried this:
httpRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
I got the status value of 400 - Bad request.
Finally figured this one out. Below is my code in case some poor minion needs to do this...maybe it will lessen the pain and shorten the time to get it to run.
I connected a WCF web service (file extension, .svc) to a classic .asp file.
I had to add a basicHttpBinding to the web service. It can also be secure (HTTPS).
This is the binding added to the service config. file:
(Note the name of it. defined by address attribute.
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
</endpoint>
Added this to the bindings part of the config file:
<basicHttpBinding>
<binding name="BasicHttpBinding_IPaging"></binding>
</basicHttpBinding>
You need to recompile the project so the WSDL has this binding in its definition.
Now the fun part....classic asp :( written in VBScript.
The SOAP envelope gave me the most trouble:
'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace. If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
'This should also be in your WSDL. Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"
'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"
'This was the hard part for me. Defining the damn soap message
'XML DOM objects.
Dim Envelope, Body, Operation
Dim ParamUserID, ParamUserName,
'Creates an XML DOM object.
Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
objXmlDoc.async = false
'Creates the main elements.
Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
objXmlDoc.appendChild Envelope
Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
Envelope.appendChild Body
'Creates an element for the SendPageForGalvanonSystem function.
Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
Body.appendChild Operation
'Add all the parameters to the DOM
Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
ParamUserID.text = strUserID
Operation.appendChild ParamUserID
Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
ParamUserName.text = strUserName
Operation.appendChild ParamUserName
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
httpRequest.Open "POST", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml"
httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
httpRequest.send objXmlDoc.xml
'Releases the objects.
Set ParamUserID = Nothing
Set ParamUserName = Nothing
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing
I have a classic ASP website hosted at Azure using Websites.
Now I need this site to access a set of WebServices located externally... I wrote a quick/dirty ASP page to check and it always times out... It seems as it was blocked.
Can anybody point me in the right direction? Please find below my quick test code.
Dim objRequest, objXMLDoc, objXmlNode
Dim strRet, strError, strNome
Dim strName
strName= "?FromCurrency=USD&ToCurrency=BRL"
Set objRequest = Server.createobject("MSXML2.XMLHTTP")
With objRequest
.open "GET", "http://www.webservicex.net/CurrencyConvertor.asmx" & strName, False
.setRequestHeader "Content-Type", "text/xml"
.setRequestHeader "SOAPAction", "http://www.webservicex.net/CurrencyConvertor.asmx"
.send
End With
Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false
Response.ContentType = "text/xml"
Response.Write(objRequest.ResponseText)
I have to call a REST service from VB6. I followed this post
Get/post to RESTful web service but doesnt able to achieve much.
What I have done so far is :
Dim sUrl As String
Dim response As String
Dim xmlhttp
sUrl = "myserviceurl"
xmlhttp = CreateObject("MSXML2.xmlhttp")
xmlhttp.open "GET", sUrl, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Set xmlhttp = Nothing
Have added MSXML6 to my project references.
I get this error :
Object doesnt support this Method..
Try below code
Dim xmlhttp As MSXML2.XMLHTTP
Set xmlhttp = New MSXML2.XMLHTTP
i am beginner in coding.
I have to call a web service below:
http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit
enter a value like 25 and click Invoke returns you the temperature in Fahrenheit.
For that i used below code:
url = "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit&Celsius=25"
'Set oHttpReq = CreateObject("Microsoft.XMLHTTP") 'when i use XMLHTTP i am getting error saying "The download of the specified resource has failed."
Set oHttpReq = CreateObject("MSXML2.ServerXMLHTTP") 'If i use it, the response contains Root Element missing
oHttpReq.open "POST", url, False
oHttpReq.send
'Response
responseText = oHttpReq.responseText
WScript.echo responseText
Can anyone help me?
Create at file.vbs (visual basic script)
Compile with external tool to exe
In server task set this.
Const HOST = Sample service in IIS
Const URL = "wsPage.asmx"
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "POST", HOST & URL & "/wsServiceTest", false
'Set the Content-Type header to the specified value
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Send the request synchronously
xmlhttp.send ""
WScript.Echo "Load ok..."