Operation timed out in xmlhttp.Send in VBA - excel

I am fetching data from website. It was working my laptop till last week without any trouble anytime, All of the sudden, it started to give strange behavior(It works in evening and it fail to work during day time. Can anyone please look into below sample code and let me know the reason?
Sub FailedTest2()
Dim xmlhttp As Object
'Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") ' I tried this also
URL = "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=HDFCBANK&instrument=FUTSTK&expiry=26DEC2019"
xmlhttp.Open "GET", URL, False
xmlhttp.setRequestHeader "Content-Type", "text/JSON"
xmlhttp.Send
MsgBox xmlhttp.responseText
End Sub

Related

VBA: Runtime Error 1004 debugging - pulling weather data from API into an Excel sheet

I'm using the "How to build a simple weather app" tutorial from Coding Is Love, with slight tweaks.
I'm getting an error 1004 when running.
My Macro
Sub Weather()
Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String, xmlresponse As New MSXML2.DOMDocument60
myurl = "http://api.openweathermap.org/data/2.5/weather?apikey=4a2360d14bf33378079d2e2d49e35ddb&mode=xml&units=imperial&q=new%20york"
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
xmlresponse.LoadXML (xmlhttp.responseText)
Sheets(1).Range(B2).Value = xmlresponse.SelectNodes("//current/temperature/#value")(0).Text
Sheets(1).Range(B3).Value = xmlresponse.SelectNodes("//current/precipitation/#mode")(0).Text
Sheets(1).Range(B4).Value = xmlresponse.SelectNodes("//current/wind/speed/#name")(0).Text
End Sub
Where do I need to make changes for it to print the values I'm looking for (#value #mode #name)?
Thank you very much for your help.

POST api authentication in vba

I am trying to connect excel (through vba) to the "teamleader" api v1, via post calls.
api docs here
I tried some solutions which I have found, regarding post calls in vba, but seem to be getting nowhere.
This is what I have so far:
Sub test2()
Dim url As String
Dim objhttp As Object
Set objhttp = CreateObject("MSXML2.ServerXMLHTTP")
url = "https://app.teamleader.eu/api/helloWorld.php"
With objhttp
.Open "POST", url, False
.setRequestHeader "api_group", "xxx"
.setRequestHeader "api_secret", "xxx"
.send
Debug.Print .responseText
End With
End Sub
This however gives me this response:
{"status":"failed","reason":"Please set api_group."}
Does anyone have any idea how to get this to work?
Turns out I needed the put all the authentication in the url itself, this question can be closed.

HTTP request Timed out

I'm trying to check a webpage if it is logged in or not. For that I used win http GET request for google to test it. But i'm getting
"The Operation Timed Out" Error
after Send command.
Also Tried with xmlHTTP and serverxmlHTTP.
Dim xmlHTTP As Object
Dim html As Object
Dim url As String
url = "https://www.google.com/"
'
'Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
xmlHTTP.SetTimeouts 10000, 10000, 10000, 10000
xmlHTTP.Open "GET", url, False
'xmlHTTP.setRequestHeader "Content-Type", "text/xml"
xmlHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = xmlHTTP.responseText
"The Operation Timed Out" Error after Send command.

Error handling with XMLHTTP on link not existing

I use XMLHTTP to connect to a API of a webserver and retrieve some data.
I connect to a http url so I can access from every position, but I want to do that when I am in office, if there is no internet connection, the macro is continuing to working connecting to a second url (internal IP).
I was thinking something like that:
Dim basic_url, basic_url_web, basic_url_local As String
basic_url_web = "www.notexistingurl.com"
basic_url_local = "192.168.178.3"
Dim myurl As String
On Error Resume Next
myurl = basic_url_web + "/api/product/read_product.php?id=4"
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
If Err.Number <> 0 then
myurl = basic_url_local + "/api/product/read_product.php?id=4"
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
END IF
On Error GoTo 0 'error checkin restored
But it is not working, actually when I call the xmlhttp.Send function I ever have a -2147467259 error number, and this is the same error number I have if actually there is no error.
If I don't use the "On Error Resume Next", then I have automation error on the .send() function (on an inexistent link), and therefore I cannot work with it.
I'm becoming crazy, I took about only 2-3 hours to do all the connection with the API and everything was working perfectly, but now I'm working since days to find a solution to have a second URL if the first cannot be reached.
Thank you for your suggestions

Establishing an API session with xmlHttp in VBA

I apologize if my question's title is incorrect - I'm used to the idea of PHP sessions with APIs.
I'm trying to accomplish the same feat in VBA with the following code:
'Login
strLogin = "https://URL.COM/authenticateUser?login=username&apiKey=password"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strLogin
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
'Open URL and get JSON data
strUrl = "https://URL.COM/Search/search?searchTerm=" & Keyword & "&mode=beginwith"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strUrl
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
Sheets(1).Cells(20, 2).Value = strReturn
With this API, I need to login first prior to executing any calls that will return data.
My problem is that I cannot determine how to "stay logged in" so that my second call works.
after I login, strReturn is populated with the following string:
{"Status":{"Code":"2","Message":"Authentication Succeeded","Success":"true"}}
However, when I go to utilize strUrl, I get the following message:
{"Status":{"Code":"1","Message":"Invalid User Name Or Password","Success":"false"}}
I have used this code in prior projects where I needed to supply an API key along with the URL for each request to the server - so this obviously worked fine. I'm not sure how to achieve the concept of "establishing a session" though with xmlHttp.
So, to anyone else who runs across this, the simple solution was to remove the following line from the second call:
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
By not creating a new object, vba is able to keep and use the cookie from the first login call.
The final code looks like this:
'Login
strLogin = "https://URL.COM/authenticateUser?login=username&apiKey=password"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strLogin
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
'Open URL and get JSON data
strUrl = "https://URL.COM/Search/search?searchTerm=" & Keyword & "&mode=beginwith"
xmlHttp.Open "GET", strUrl
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
Sheets(1).Cells(20, 2).Value = strReturn
And the API which requires a login is able to keep the session established.

Resources