MSXML2.ServerXMLHTTP - can't access website - vba

For instance, kat.cr is one of the few websites I cannot access.
In the response, I get 2 symbols instead of the actual webpage:
Here's the VBA code I'm using:
url = "https://kat.cr"
Set xmlHTTP = CreateObject("MSXML2.serverXMLHTTP")
xmlHTTP.Open "GET", url, False
xmlHTTP.setRequestHeader "Accept-Language", "en-US,en;q=0.8"
xmlHTTP.setRequestHeader "Content-Type", "text/xml"
xmlHTTP.Send
Set html = CreateObject("htmlfile")
response = xmlHTTP.responseText
Is the website actually denying me access or am I doing something wrong?

Use CreateObject("MSXML.XMLHTTP") for client applications. ServerXMLHttp is meant for server applications. See this article for more information on the two.
Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
xmlHTTP.Open "GET", "https://kat.cr", False
xmlHTTP.Send
Debug.Print xmlHTTP.responseText

The website is not denying you access, it's your code I am afraid. Below is a quick example to get the HTML you want from the page you visit.
Note: This was just a quick type-o, but to help get you in the right direction.
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "https://kat.cr"
Set html = ie.document
'this is the inner html -html.DocumentElement.innerHTML
Set ie = Nothing
Edit - This might be a better solution for you
Set xmlHTTP = New MSXML2.XMLHTTP
xmlHTTP.Open "GET", "https://kat.cr", False
xmlHTTP.send
Dim doc As Object
Set doc = CreateObject("htmlfile")
doc.body.innerHTML = xmlHTTP.responseText
debug.print doc.body.innerHTML

Related

Post to RESTful web service

I have this code but i cant seem to get around how to use same code with body parameters. I have to add two body parameters of number = 123456 and first_name = james. Please help update the following code to allow that and work within VB6.
Dim sUrl As String
Dim response As String
Dim xmlhttp
Set sUrl = "http://my.domain.com/service/operation/param"
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", sURL, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send()
Dim response As String = xmlhttp.responseText
Set xmlhttp = Nothing

"LOADING" SOAP response to DOMDocument

After some help from stackoverflow experts I have been able to successfully retrieve my response using SOAP. The below piece is how I received and stored the data. This of course is not all the code. I just included this to show how I later reference the xml.
With xmlhtp
webserviceSOAPActionNameSpace = "http://example.com/webservices/"
.Open "POST", sUrl, False
.setRequestHeader "POST", "https://onesite.example.com/webservices/stuff.asmx HTTP/1.1"
.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
.setRequestHeader "Content-Length", 100
.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "RetrieveData"
.send sEnv
sResult = xmlhtp.statusText
responseText = xmlhtp.responseText
ActiveSheet.Cells(1, 1).Value = .responseText
End With
Debug.Print responseText
Now I am having trouble parsing that out. This seems like it should be pretty simple but I get an error indicating that the responseText I receive above is not "loading" to xmlDOC. The following is at the beginning of the sub:
Dim xmlhtp As New MSXML2.XMLHTTP
Dim xmlDoc As New DOMDocument
Dim XDoc As Object
After the With End (shown above) my code looks like this:
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (xmlhtp.responseText)
Set lists = XDoc.DocumentElement
Set getFirstChild = lists.FirstChild
Debug.Print getFirstChild.XML
Debug.Print getFirstChild.Text
On the line
Set getFirstChild = lists.FirstChild
I recieve the following error
Object variable or With block variable not set
When I look at the Local Variable window in VBA I can clearly see that nothing was assigned to xmlDoc. So I assume my problem is in XDoc.Load Line.
Any direction would be appreciated.
use XDoc.LoadXML (xmlhtp.responseText) instead of XDoc.Load (xmlhtp.responseText)

Add an attachment using: api/2/issue/{issueIdOrKey}/attachments

I have successfully been able to create a Jira Issue, now want to to add an attachment using: api/2/issue/{issueIdOrKey}/attachments
My following VB code wont work, any suggestions please as this is new to me, thanks. The error I get is: HTTP Status 500 - Error parsing Content-Type
Sub JIRA_PostAttachment()
Dim pHtml As String
Dim oHttp As Object
Dim strResponse As String
pHtml = "https://jira.ae.sda.corp.test.com/rest/api/2/issue/IS-163/attachments"
sVar = """file="": ""C:\Users\c776469\FORM.msg"""
Set oHttp = CreateObject("Microsoft.XMLHTTP")
Call oHttp.Open("POST", pHtml, False)
'oHttp.SetRequestHeader "Content-Type", "application/json"
oHttp.SetRequestHeader "Content-Type", "multipart/form-data;Charset=UTF-8; boundary="
'oHttp.SetRequestHeader "Accept", "application/json"
oHttp.SetRequestHeader "X-Atlassian-Token", "no-check"
oHttp.SetRequestHeader "Authorization", "Basic Yzc4NjQ3OTpHaWxpdDIwMTY/"
Call oHttp.Send(sVar)
strResponse = oHttp.ResponseText
MsgBox strResponse
Set oHttp = Nothing
End Sub
Please look at this link:
https://answers.atlassian.com/questions/39127148/answers/39127815/comments/39130956
It has a VBA example that works (also a C# example)

Login into website using MSXML2.XMLHTTP instead of InternetExplorer.Application with VBA

first time posting,
I'm trying to get the ID "dadosDoUsuario" from a website's page I have to be logged in. I got it working using "InternetExplorer.Application" object, but can't get the ID value when using "MSXML2.XMLHTTP" object. It seems it won't go past the login page, since I'm able to get other IDs from this page (example: "tituloPagina"). Could someone give a hint on how I get the data from the page after logged in? Thanks!
InternetExplorer.Application code (this one works):
Sub testIE()
Dim texto As String
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://www.nfp.fazenda.sp.gov.br/login.aspx"
With ie
.Visible = False
.Navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
ie.Document.getelementbyid("userName").Value = "MYUSERNAME"
ie.Document.getelementbyid("Password").Value = "MYPASSWORD"
ie.Document.getelementbyid("Login").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getelementbyid("btnConsultarNFSemestre").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
texto = ie.Document.getelementbyid("dadosDoUsuario").innerText
MsgBox texto
ie.Quit
End Sub
MSXML2.XMLHTTP code (this one doesn't work):
Sub testXMLHTTP()
Dim xml As Object
Dim html As Object
Dim dados As Object
Dim text As Object
Set xml = CreateObject("MSXML2.XMLHTTP")
Set html = CreateObject("htmlFile")
With xml
.Open "POST", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
.setRequestHeader "Content-Type", "text/xml"
.send "userName=MYUSERNAME&password=MYPASSWORD"
.Open "GET", "https://www.nfp.fazenda.sp.gov.br/Inicio.aspx", False
.setRequestHeader "Content-Type", "text/xml"
.send
End With
html.body.innerhtml = xml.responseText
Set objResult = html.GetElementById("dadosDoUsuario")
GetElementById = objResult.innertext
MsgBox GetElementById
End Sub
EDIT: I followed the steps suggested by #Florent B., and added a scripcontrol to get the encoded values for __VIEWSTATE, __VIEWSTATEGENERATOR and __EVENTVALIDATION. Got it working!
Sub testXMLHTTP()
Dim xml As Object
Dim html As HTMLDocument
Dim dados As Object
Dim text As Object
Dim html2 As HTMLDocument
Dim xml2 As Object
Set xml = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Set html = CreateObject("htmlFile")
With xml
.Open "GET", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
.send
End With
strCookie = xml.getResponseHeader("Set-Cookie")
html.body.innerhtml = xml.responseText
Set objvstate = html.GetElementById("__VIEWSTATE")
Set objvstategen = html.GetElementById("__VIEWSTATEGENERATOR")
Set objeventval = html.GetElementById("__EVENTVALIDATION")
vstate = objvstate.Value
vstategen = objvstategen.Value
eventval = objeventval.Value
'URL Encode ViewState
Dim ScriptEngine As ScriptControl
Set ScriptEngine = New ScriptControl
ScriptEngine.Language = "JScript"
ScriptEngine.AddCode "function encode(vstate) {return encodeURIComponent(vstate);}"
Dim encoded As String
encoded = ScriptEngine.Run("encode", vstate)
vstate = encoded
'URL Encode Event Validation
ScriptEngine.AddCode "function encode(eventval) {return encodeURIComponent(eventval);}"
encoded = ScriptEngine.Run("encode", eventval)
eventval = encoded
'URL Encode ViewState Generator
ScriptEngine.AddCode "function encode(vstategen) {return encodeURIComponent(vstategen);}"
encoded = ScriptEngine.Run("encode", vstategen)
vstategen = encoded
Postdata = "__EVENTTARGET=" & "&__EVENTARGUMENT=" & "&__VIEWSTATE=" & vstate & "&__VIEWSTATEGENERATOR=" & vstategen & "&__EVENTVALIDATION=" & eventval & "&ctl00$ddlTipoUsuario=#rdBtnNaoContribuinte" & "&ctl00$UserNameAcessivel=Digite+o+Usuário" & "&ctl00$PasswordAcessivel=x" & "&ctl00$ConteudoPagina$Login1$rblTipo=rdBtnNaoContribuinte" & "&ctl00$ConteudoPagina$Login1$UserName=MYUSERNAME" & "&ctl00$ConteudoPagina$Login1$Password=MYPASSWORD" & "&ctl00$ConteudoPagina$Login1$Login=Acessar" & "&ctl00$ConteudoPagina$Login1$txtCpfCnpj=Digite+o+Usuário"
Set xml2 = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Set html2 = CreateObject("htmlFile")
With xml2
.Open "POST", "https://www.nfp.fazenda.sp.gov.br/Login.aspx", False
.setRequestHeader "Cookie", strCookie
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Lenght", Len(Postdata)
.send (Postdata)
End With
html2.body.innerhtml = xml2.responseText
Set objResult = html2.GetElementById("dadosDoUsuario")
GetElementById = objResult.innertext
MsgBox GetElementById
End Sub
It's possible but not that easy.
First you need to use CreateObject("Msxml2.ServerXMLHTTP.6.0") and not CreateObject("MSXML2.XMLHTTP").
Then follow these steps:
Open and send a GET to https://www.nfp.fazenda.sp.gov.br/login.aspx
Parse and store the cookie from the response header "Set-Cookie"
Parse and store the __VIEWSTATE, __VIEWSTATEGENERATOR, __EVENTVALIDATION from the HTML response
Build the data for the next query with the values parsed previously and with your user-name/password :
__EVENTTARGET:""
__EVENTARGUMENT:""
__VIEWSTATE:"..."
__VIEWSTATEGENERATOR:"..."
__EVENTVALIDATION:"..."
ctl00$ddlTipoUsuario:"#rdBtnNaoContribuinte"
ctl00$UserNameAcessivel:"Digite+o+Usuário"
ctl00$PasswordAcessivel:"x"
ctl00$ConteudoPagina$Login1$rblTipo:"rdBtnNaoContribuinte"
ctl00$ConteudoPagina$Login1$UserName:"..."
ctl00$ConteudoPagina$Login1$Password:"..."
ctl00$ConteudoPagina$Login1$Login:"Acessar"
ctl00$ConteudoPagina$Login1$txtCpfCnpj:"Digite+o+Usuário"
Open a POST to https://www.nfp.fazenda.sp.gov.br/login.aspx
Set the header "Cookie" with the cookie parsed at step 2
Set the header Content-Type: "application/x-www-form-urlencoded"
Set the header Content-Length with the length of the data
Send the POST with the data from step 4

Send data using msxml2.xmlhttp.3.0 to web to select Datepicker in EXCEL VBA

The following is the HTML of part of a web page.
"input name="ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate" type="text"
id="ctl00_ctl00_AllContent_ContentMain_ucMktStatCtl_txtDate"
onkeypress="javascript:return fnTrapKD(event, document.getElementById('ctl00_ctl00_AllContent_ContentMain_ucMktStatCtl_butReport'))"
value="02/24/2006" class="hasDatepicker">
I tried to use the following code to access the data.
Dim strPostData As String: strPostData = "ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate=02/24/2006"
Dim xmlhttp: Set xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
xmlhttp.Open "POST", "http://www.cboe.com/data/mktstat2.aspx#VIX", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send (strPostData)
I am getting responsetext with 404 - File or directory not found. But the site does accept the input in a browser.
The mozilla firefox addon firebug helps to analyse the http request.
Post tab shows the parameters which are sent.
The URL should be http://www.cboe.com/data/mktstat2.aspx
Sub test()
Dim strPostData As String
strPostData = "ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$butReport=Get Report&ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$ddlNav=&ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate=05/31/2013&ctl00$ctl00$AllContent$ucHeader$CBOEHeaderSearchBox$txtHeaderSearch=Search&ctl00$ctl00$AllContent$ucHeader$ucCBOEHeaderQuoteBox$txtHeaderQuote=Quote"
Dim xmlhttp As Object
Set xmlhttp = CreateObject("msxml2.xmlhttp")
xmlhttp.Open "POST", "http://www.cboe.com/data/mktstat2.aspx", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send (strPostData)
MsgBox xmlhttp.responseText
End Sub