xmlhttp POST isn't working - xmlhttprequest

I'm trying to post transactions to server using a middleware client (it's called Cohesion). Below is my code (asp):
Dim xmlhttp 'As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", "http://server_ip/Databox-dr/CohesionConnect.asmx/GetHostReply", False
'using only http://server_ip/Databox-dr/CohesionConnect.asmx doesn't work, either
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'xmlhttp.setRequestHeader "Content-Length", "128"
xmlhttp.send "50000000000MSUF"
Set xmlhttp = Nothing
The documentation I have (and that's all of it) is tellin me this:
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /DATABOX-DR/CohesionConnect.asmx/GetHostReply HTTP/1.1
Host: server_ip
Content-Type: application/x-www-form-urlencoded
Content-Length: length
sTran=string
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://fidelityifs.com/webservices">string</string>
(length and string need to replaced with real values)
What am I doing wrong? How can I set the string length?
Thanks!!

Try this:
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", "http://server_ip/Databox-dr/CohesionConnect.asmx/GetHostReply", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
tosend = "50000000000MSUF"
s = "sTran=" & tosend
xmlhttp.setRequestHeader "Content-Length", Len(s)
xmlhttp.send s

Related

.Write objHTTP.responseBody runtime error 3001

I have some problem with this vba code. This work from over the year. After last execution i get error in ".Write objHTTP.responseBody --runtime error 3001" I try figure out solution but I don't have any ideas. The code login in on page and download 4 files.
This is POST information:
Answers headers (250 B)
Connection
Keep-Alive
Content-Disposition
attachement; filename="baza.csv";
Content-Type
application/csv
Date
Tue, 17 Jul 2018 13:58:32 GMT
Keep-Alive
timeout=5, max=300
Server
Apache/2.4.25
Transfer-Encoding
chunked
Request Headers (976 B)
Accept
text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate, br
Accept-Language
pl,en-US;q=0.7,en;q=0.3
Connection
keep-alive
Content-Length 38
Content-Type
application/x-www-form-urlencoded
Cookie _pk_id.50.777d=00d79034e23a5b6…41fc349c56e551787285709412976
DNT 1
Host listarobinsonow.pl
Referer https://listarobinsonow.pl/userpanel/base
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/61.0
Code:
Sub SMB_DownloadAll()
Dim destfolder As String
destfolder = ActiveWorkbook.Path
' Autoryzacja
' Dim objHTTP As New WinHttp.WinHttpRequest
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://www.listarobinsonow.pl/auth/login"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("identity=xxxxxx&password=xxxxxxx")
objHTTP.WaitForResponse
' pobranie 4 plików
SMB_Download objHTTP, "post", destfolder
SMB_Download objHTTP, "mail", destfolder
SMB_Download objHTTP, "tele", destfolder
SMB_Download objHTTP, "smss", destfolder
ThisWorkbook.Save
End Sub
Sub SMB_Download(ByRef objHTTP, base_type As String, destfolder As String)
URL = "http://www.listarobinsonow.pl/userpanel/base"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("base_type=" + base_type + "&form_type=base_download")
objHTTP.WaitForResponse
' Dim oStream1 As New ADODB.Stream
Set oStream1 = CreateObject("ADODB.Stream")
With oStream1
.Type = 1
.Open
.Write objHTTP.responseBody - the place where i get error run time 3001
.SaveToFile destfolder + "\" + base_type + ".csv", 2
End With
End Sub

Amazon URL scraping with XMLHTTP - Amazon blocking?

So if I use my browser to browse to a list of a product's ellers, for example:
https://www.amazon.co.uk/gp/offer-listing/B076C6769Z/ref=dp_olp_new?ie=UTF8&condition=new
I see a list of sellers.
BUT if I construct the same URL with VBA utilising XMLHTTP, Amazon returns a different page (generic page not related to the product). it's as if they've sussed that I'm not a person using a browser?
Dim XMLHTTP As Object, html As Object, objResult As Object
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
url = "https://www.amazon.co.uk/gp/offer-listing/B002AVVO7K/ref=dp_olp_new?
ie=UTF8&condition=new"
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0)
Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.responseText
Debug.Print XMLHTTP.responseText

VBA XMLHTTP disable authentication pop-up

With reference to this question VBA XMLHTTP clear authentication? I am also trying to pass basic authentication in VBA. It works perfectly.
My only concern is login pop up which comes up whenever a wrong userid or password is sent in request. Is there a way to disable this and send a message box to user from VBA itself telling him about authentication failure?
Update
VBA code:
Dim objHTTP As New MSXML2.XMLHTTP60
With objHTTP
.Open "GET", UrlUserService, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Authorization", "Basic " + Base64Encode(userName + ":" + userPwd)
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send ""
response = .Status
End With
Popup
Use Msxml2.XMLHTTP.6.0 protocol and pass the credentials as part of request
.Open "GET", pUrl, false, "pUsername", "pPassword"

Why won't this VBA program using ServerXMLHTTP60 authenticate properly?

I have 4 different queries of IPR 1.2.3.4 (ip-reputation) to IBM's Xforce database, using basic authentication (base64 encoded), as well as URL-reputation. My version in python works great, printing out the appropriate JSON information:
...
if __name__ == '__main__':
apiKey = "1234...."
apiPwd = "5678..."
result = requests.get('https://xforce-api.mybluemix.net:443/ipr/1.2.3.4', verify=False,auth=(apiKey, apiPwd))
if result.status_code != 200:
print( "~ Bad Status Code: {}".format(result.status_code))
else:
print("~ The result is {}".format(result.status_code))
print("~ Rx Data={}".format(result._content))
The version in Go (using demisto's goxforce from github) works great. After setting my environment-variable key and password, I issue the commandline:
'xforceQuery -cmd ipr -q 1.2.3.4'
and it prints out the json information about 1.2.3.4, again, perfectly.
I use the browser-utility called 'Postman', specify basic authentication with my user/key and password, headers of Accept: application/json, Accept-Language: en, and Content-Type: application/json, and, again, it gives me the proper information (see .gif, below)
On the other hand, I try the same thing in VBA, and I get '401 Error: Not authorized'. What's wrong with this code?
Public Sub testXF()
Dim myKey As String
Dim myPass As String
myKey = "1234..."
myPass = "5678..."
pHtml = "xforce"
Dim ohttp As MSXML2.ServerXMLHTTP60
Set ohttp = New MSXML2.ServerXMLHTTP60
If timeout = 0 Then timeout = 60
ohttp.Open "GET", "https://xforce-api.mybluemix.net:443/ipr/1.2.3.4", False, myKey, myPass
With ohttp
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Accept", "application/json"
.SetRequestHeader "Accept-Language", "en"
.SetTimeouts 0, 30 * 1000, 30 * 1000, timeout * 1000
.SetRequestHeader "Authorization", "Basic " + _
Base64Encode(myKey + ":" + myPass)
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
End With
ohttp.Send ("")
With ohttp
pStatus = .status
pText = .ResponseText
pResponseHeaders = .GetAllResponseHeaders()
End With
Debug.Print "GET", pStatus, pText
Set ohttp = Nothing
End Sub

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