Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

come inviare un dato ad un web service

139 views
Skip to first unread message

pfmarro

unread,
May 21, 2021, 4:54:11 AM5/21/21
to
salve ho una mia applicazione in access che effettuare un aggiornamento di record all'inserimento di un dato in una maschera
(eseguo una query di aggiornamento)

mi chiedono ora
E’ possibile che ad ogni lettura che viene fatta, venga invocato un Web service pubblicato e passare semplicemente il codice letto?

potreste darmi un suggerimento?
non ho mai inviato dati ad un Web service

GRAZIE

BFS

unread,
May 21, 2021, 7:29:25 AM5/21/21
to
in generale dipende dal webserver, avra un sintassi e dei parametri da
rispettare

al lato pratico si tratta di inviare un link e leggersi i risultati
restituti

ad esempio in una applicazione che sto facendo devo chiedere a google
maps di darmi la distanza di n destinazioni da un punto precisato
costruisco il mio link:

strQuery="https://maps.googleapis.com/maps/api/distancematrix/xml? bla
bla bla + eventuale apikey"

Dim googleResult As New MSXML2.DOMDocument60
Dim googleService As New MSXML2.XMLHTTP60

'qui mandi la richiesta
googleService.Open "GET", strQuery, False
googleService.Send
'qui ti leggi il risultato
googleResult.loadXML (googleService.responseText)


in questo caso mi viene restituito un xml con la distanza in km e in
minuti dei vari luoghi
nel tuo caso ci sara una strQuery costruita a hoc per passare tale
parametro e dato di ritorno con l'ok o meno del dato inserito


riferimento a xml 6.0

ciao
BFS


@Alex

unread,
May 21, 2021, 7:52:33 AM5/21/21
to

pfmarro

unread,
May 21, 2021, 8:37:43 AM5/21/21
to
grazie nel mio caso dovrei inviare 2 o 3 variabili

dData as date
nValore as integer
sCodice as string



mi scrivono "la funzione da chiamare è ‘ReadLabel’, che richiede un valore int ( numero del ballotto da barcode ) e un boolean, che passerai sempre a false.
"
abbiate pazienza ma non l'ho mai fatto

ho trovato del codice ma non so se va bene

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://www.somedomain.com"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send("")

come faccio a richiamare la funzione ReadLabel?
e a inviare i 3 valori?

Grazie anticipatamente

GRAZIE


BFS

unread,
May 21, 2021, 9:26:00 AM5/21/21
to
ma non hai una documentazione su come costruire la chiamata?

esempio
il portale di ticketing che uso mette a disposizione un webservices
se voglio ad esempio aggiungere un contatto da codice mi vedo qui
l'istruzione

https://developers.freshdesk.com/api/#create_contact


e il mio codice vba per richiamare quella funzione è

Public Function APIfleshdesk(URL As String, TIPO As String, BODY As
String) As String


Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP")
With xmlhttp

.Open "POST", "https://xxxx.freshdesk.com/api/v2/contacts", False
.SetRequestHeader "User-Agent", "curl/7.33.0"
.SetRequestHeader "Authorization", "Basic " &
Base64EncodeString("0xxxxxxxq:api_token")
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "body", "qui metto i parametri che servono"

If BODY <> "" Then
.Send BODY
Else
.Send
End If


Debug.Print .ResponseText
APIfleshdesk = .ResponseText

End With


End Function


non è che puoi mettere valori a caso.
magari ti basta anche semplicemente aprire un link appositamente
formattato per attivare lo script del webservices

BFS



BFS

unread,
May 21, 2021, 9:31:37 AM5/21/21
to
Il 21/05/2021 14:37, pfmarro ha scritto:
sempre un altro esempio che magari si avvicina al tuo caso
qui contatto il webservices di skebby per sapere il credito di sms di
una utenza

i valori che passo io sono user e psw
e lui mi restituisce il credito

Function skebbyGatewayGetCredit(UserName As String, Password As String,
Optional charset As String = "") As String
Dim url, method, parameters As String

Dim xmlhttp As Object

url = "http://gateway.skebby.it/api/send/smseasy/advanced/http.php"
method = "get_credit"

parameters = "method=" & method & "&" _
& "username=" & URLEncode(UserName) & "&" _
& "password=" & URLEncode(Password)

Select Case charset
Case "UTF-8"
parameters = parameters & "&charset=UTF-8"
Case Else
End Select

Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")

xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Length", Len(parameters)
xmlhttp.Send parameters

If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
skebbyGatewayGetCredit = "status=failed&message=" &
xmlhttp.Status & " - " & xmlhttp.statusText
Exit Function
End If

msg = xmlhttp.responseText
Set xmlhttp = Nothing

skebbyGatewayGetCredit = msg

End Function



Alessandro Cara

unread,
May 21, 2021, 4:29:44 PM5/21/21
to
Azz! ed io che ho sempre pensato che, di fatto, il web service fosse una
chiamata a funzione!
Chiami un tizio che sta in Antartide passando parametri ed aspetti un
risultato.
;-)


--
ac (y-1=x)

pfmarro

unread,
May 24, 2021, 9:53:03 AM5/24/21
to
salve
grazie delle dritte
mi hanno dato il link al web server e user + password
dal browser riesco ad accedere e mi visualizza un file xml

dalla mia procedura mi dà errore di autenticazione
potete darmi una dritta?
grazie




'
' Ho provato a pubblicare questo WS: ( url SOAP )

' Il link ovviamente cambierà quando saremo in live su altri db o società, ma il comportamento ovviamente resterà lo stesso.
' la funzione da chiamare è ‘ReadLabel’, che richiede un valore int e un boolean, che passerai sempre a false.
' Dovrebbe rispondere true o false a seconda di ballotto trovato e letto oppure no.
'


Public Sub pfm2_WebServer_Test()
Dim sUrl As String
Dim sUser As String
Dim sPassword As String
Dim nBallotto As int

sUrl = "http://link al webserver.."
sUser = "dominio\utente"
sPassword = "miapassword"

nBallotto =400

Call PFM_SEND_BALLOTTO(sUrl, sUser, sPassword, sBallotto)
End Sub

Function PFM_SEND_BALLOTTO(url As String, _
UserName As String, Password As String, _
Optional charset As String = "") As String
Dim xmlhttp As Object

Dim method, parameters As String

method = "ReadLabel"

parameters = "method=" & method & "&" & _
"username=" & URLEncode(UserName) & "&" & _
"password=" & URLEncode(Password)

Select Case charset
Case "UTF-8"
parameters = parameters & "&charset=UTF-8"
Case Else
End Select

Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")

xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Length", Len(parameters)
xmlhttp.send parameters

If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
PFM_SEND_BALLOTTO = "status=failed&message=" & _
xmlhttp.Status & " - " & xmlhttp.statusText
MsgBox "Failed " & PFM_SEND_BALLOTTO

Exit Function
End If

Dim msg As String
msg = xmlhttp.responseText

Debug.Print msg
Set xmlhttp = Nothing

PFM_SEND_BALLOTTO = msg

End Function


' riferimento https://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba
' Microsoft ActiveX Data Objects 2.8 Library.
' riferimento Microsoft ActiveX Data Objects x.x Library
'
Public Function URLEncode( _
ByVal StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String

Dim bytes() As Byte, b As Byte, i As Integer, space As String

If SpaceAsPlus Then space = "+" Else space = "%20"

If Len(StringVal) > 0 Then
With New ADODB.Stream
.Mode = adModeReadWrite
.Type = adTypeText
.charset = "UTF-8"
.Open
.WriteText StringVal
.Position = 0
.Type = adTypeBinary
.Position = 3 ' skip BOM
bytes = .Read
End With

ReDim result(UBound(bytes)) As String

For i = UBound(bytes) To 0 Step -1
b = bytes(i)
Select Case b
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
result(i) = Chr(b)
Case 32
result(i) = space
Case 0 To 15
result(i) = "%0" & Hex(b)
Case Else
result(i) = "%" & Hex(b)
End Select
Next i

URLEncode = Join(result, "")
End If
End Function

BFS

unread,
May 25, 2021, 3:22:54 AM5/25/21
to
Il 24/05/2021 15:53, pfmarro ha scritto:
> salve
> grazie delle dritte
> mi hanno dato il link al web server e user + password
> dal browser riesco ad accedere e mi visualizza un file xml
>
> dalla mia procedura mi dà errore di autenticazione
> potete darmi una dritta?
> grazie
>
>

se incollando il link nel browser funziona vuol dire che sbagli tu a
costuirlo via vba o a richiamarlo

puoi incollare qui questo link oscurando la psw?



BFS

pfmarro

unread,
May 25, 2021, 4:12:11 AM5/25/21
to
il web service è locale

mi dicono che SOAP

BFS

unread,
May 25, 2021, 5:39:40 AM5/25/21
to
ma è un segreto o si può vedere la documentazione che ti hanno fornito?
è impossibile aiutarti altrimenti

dovresti avere l'url da richiamare
eventuali parametri per l'header
varie ed eventuali regole da rispettare


quando hai questi valori è una boiata richamare la funzione, caricarti
l'xml con il risultato e leggerti il nodo che ti interessa

BFS


@Alex

unread,
May 25, 2021, 5:42:58 AM5/25/21
to
...
> il web service è locale
>
> mi dicono che SOAP

Ecco pensa un poco... SOAP... ;-)

@Alex

pfmarro

unread,
May 25, 2021, 10:33:15 AM5/25/21
to
ecco cosa mi hanno scritto

'
'I parametri sono 2,
'il numero ( intero ) del ballotto e un secondo parametro fisso con un false ( o zero ).
'Se vuoi posso provare a farti una funzione semplificata dove passi solo una stringa con il numero del ballotto e basta..
'Dovrebbe rispondere un true/false a seconda che sia riuscito a leggere il ballotto o meno.
'
'
'siamo riusciti a risolvere l’errore 401.
'La connessione standard usa ‘basic authentication’.
'In questo caso occorre usare ‘NTLM Authentication’
'Attivandola l’errore sparisce e ci si connette.
'Il mio collega mi dice che nel tuo codice ( Visual basic? ) dovrebbe risolversi in teoria in questo modo.. aggiungendo il settaggio in rosso
'


ecco la fuzione con i paramteri che richiama l'altra
Public Sub pfm2_WebServer_Test()
Dim sUrl As String
Dim sUser As String
Dim sPassword As String
Dim nBallotto As Integer

sUrl = "http://myLocal..../BalesMgt"
sUser = "DOMGRP\Pippo"
sPassword = "Prova1234"

nBallotto = 400

Call PFM_SEND_BALLOTTO(sUrl, sUser, sPassword, nBallotto)
End Sub

Function PFM_SEND_BALLOTTO(url As String, _
UserName As String, Password As String, _
nVal As Integer) As String
Dim xmlhttp As Object

Dim method, parameters As String

method = "ReadLabel"

parameters = "method=" & method

Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")


xmlhttp.Open "POST", url, False
xmlhttp.NtlmAuth = True ' aggiunto

xmlhttp.setRequestHeader "Authorization", "Basic " & EncodeBase64(UserName & ":" & Password)
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' xmlhttp.setRequestHeader "Content-Length", Len(parameters)
xmlhttp.send ' parameters

If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
PFM_SEND_BALLOTTO = "status=failed&message=" & _
xmlhttp.Status & " - " & xmlhttp.statusText
MsgBox "Failed " & PFM_SEND_BALLOTTO

Exit Function
End If

Dim msg As String
msg = xmlhttp.responseText

Debug.Print msg
Set xmlhttp = Nothing

PFM_SEND_BALLOTTO = msg

End Function


ma mi dà sempre Failed status = 401 unauthorized

eppure se vado tramite un browser e digito il link dell'url di apre una form con nome utente e password. se digito si apre un file XML

pfmarro

unread,
May 26, 2021, 6:14:50 AM5/26/21
to
salve
per testare il funziomento del SOAP ho trovato un sito
https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords

ho provato a scrivere questo codice che mi produce un envelope

ma eseguendola mi dice "Request Format is invalid"

qualcuno varebbe la cortesia di testare questo codice?

' DataAccessNumberConversion

' Fonte: https://learning.postman.com/docs/sending-requests/supported-api-frameworks/making-soap-requests/
' richiesta --> https://www.dataaccess.com/webservicesserver/NumberConversion.wso


''Public Sub pfmLiving_WebServer_Test()
'' Dim sURL As String
'' Dim sUser As String
'' Dim sPassword As String
'' Dim nBallotto As Integer
''
'' sURL = "http://07sgest-app.dentisgrp.local:8147/BC180_DENTIS_TEST/WS/Dentis%20Recycling%20srl%20TEST01042/Codeunit/BalesMgt"
'' sUser = "DentisGRP\Velika5"
'' sPassword = "Dentis2021"
''
'' nBallotto = 400 ' "100012345"
''
'' Call pfmLiving_InvokeAuthenticate(sURL, sUser, sPassword, nBallotto)
''End Sub

Public Function pfmNumberConversion()
Dim sURL As String
Dim sUser As String
Dim sPassword As String
Dim nNumero As Long

'sURL = "http://07sgest-app.dentisgrp.local:8147/BC180_DENTIS_TEST/WS/Dentis%20Recycling%20srl%20TEST01042/Codeunit/BalesMgt"
sURL = "https://www.w3schools.com/xml/tempconvert.asmx/FahrenheitToCelsius"


sUser = "DentisGRP\Velika5"
sPassword = "Dentis2021"

nNumero = 400 ' "100012345"

Call TEST_NumberConversion(sURL, sUser, sPassword, nNumero)

End Function


Public Function TEST_NumberConversion(sURL, sUser, sPassword, nNumero) As String

'POST /webservicesserver/NumberConversion.wso HTTP/1.1
'Host: www.dataaccess.com
'Content-Type: text/xml; charset=utf-8
'Content -length: length
'
'<?xml version="1.0" encoding="utf-8"?>
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
' <soap:Body>
' <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
' <ubiNum>unsignedLong</ubiNum>
' </NumberToWords>
' </soap:Body>
'</soap:Envelope>

'Declare our working variables
Dim sMsg As String


Dim sEnv As String
Dim LastToken As String

Dim objhttp As Object

Set objhttp = CreateObject("MSXML2.XMLHTTP")


'SOAP envelope for submission to the Web Service
sEnv = ""

sEnv = sEnv & "POST /webservicesserver/NumberConversion.wso HTTP/1.1"
sEnv = sEnv & "Host: www.dataaccess.com"
sEnv = sEnv & "Content-Type: text/xml; charset=utf-8"
sEnv = sEnv & "Content -length: length"
sEnv = sEnv & ""

sEnv = sEnv & "<?xml version=""1.0"" encoding=""utf-8""?>"
sEnv = sEnv & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
' sEnv = sEnv & " <soap:Header/>"
' sEnv = sEnv & " </soap:Header/>"
sEnv = sEnv & " <soap:Body>"
'sEnv = sEnv & " <ns:Authenticate>"
'Env = sEnv & " <ns:userName>notdisplayed</ns:userName>"
'sEnv = sEnv & " <ns:password>notdisplayed</ns:password>"
'sEnv = sEnv & " <ns:userName>" & sUser & "</ns:userName>"
'sEnv = sEnv & " <ns:password>" & sPassword & "</ns:password>"
'sEnv = sEnv & " </ns:Authenticate>"

sEnv = sEnv & " <NumberToWords xmlns=""http://www.dataaccess.com/webservicesserver/"">"
sEnv = sEnv & " <ubiNum>" & nNumero & "</ubiNum>"
sEnv = sEnv & " </NumberToWords>"

sEnv = sEnv & " </soap:Body>"
sEnv = sEnv & " </soap:Envelope>"
'invoke the web service
objhttp.Open "POST", sURL, False

objhttp.setRequestHeader "Content-Type", "text/xml"
objhttp.send (sEnv)

MsgBox objhttp.responseText

If objhttp.Status >= 400 And objhttp.Status <= 599 Then
TEST_NumberConversion = "status=failed&message=" & _
objhttp.Status & " - " & objhttp.statusText
MsgBox "Failed " & TEST_NumberConversion

Exit Function
End If
End Function

BFS

unread,
May 26, 2021, 9:41:02 AM5/26/21
to
per quel wenservices ti basta :

Dim body As String
body = "<soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><NumberToWords
xmlns='http://www.dataaccess.com/webservicesserver/'><ubiNum>12</ubiNum></NumberToWords></soap:Body></soap:Envelope>"


Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP")
With xmlhttp

.Open "POST",
"https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords",
False
.SetRequestHeader "Content-Type", "text/xml; charset=utf-8"
.Send body

Debug.Print .ResponseText
End With




invio 12 e come risultato mi spara :


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<m:NumberToWordsResponse
xmlns:m="http://www.dataaccess.com/webservicesserver/">
<m:NumberToWordsResult>twelve </m:NumberToWordsResult>
</m:NumberToWordsResponse>
</soap:Body>
</soap:Envelope>



qui ti basta leggere il nodoNumberToWordsResult che contiene "twelve"

ciao
BFS





pfmarro

unread,
May 26, 2021, 10:36:56 AM5/26/21
to
GRAZIE e ho testo e funziona

mi suggerisci di come estrapolare il risultato da XML?

GRAZIE

BFS

unread,
May 26, 2021, 11:02:41 AM5/26/21
to
aggiungi il riferimento a microsoft xml v6


Dim body As String
Dim NumeroDaConvertire As Integer

NumeroDaConvertire = 4512

body = "<soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><NumberToWords
xmlns='http://www.dataaccess.com/webservicesserver/'><ubiNum>" &
NumeroDaConvertire & "</ubiNum></NumberToWords></soap:Body></soap:Envelope>"

Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP")

Dim mioXML As New MSXML2.DOMDocument60
With xmlhttp

.Open "POST",
"https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords",
False
.SetRequestHeader "Content-Type", "text/xml; charset=utf-8"
.Send body
Debug.Print .ResponseText
mioXML.loadXML (.ResponseText)
MsgBox mioXML.Text
End With



in questo caso te la cavi rapidamente con
MsgBox mioXML.Text

se l'xml fosse con nodi e sottonodi con vari valori il discorso cambia

ciao
BFS



0 new messages