WebService WCF

HMG Unicode versions 3.1.x related

Moderator: Rathinagiri

Post Reply
User avatar
Carlos Reis
Posts: 16
Joined: Sat Oct 09, 2010 3:31 pm
Location: Portugal

WebService WCF

Post by Carlos Reis » Fri Jul 25, 2014 3:51 pm

Hello everyone
I need a little help with a problem for which I do not feel very at ease.
My version of HMG is 3.2 and I need to work with a WebService WCF
The webservice is created and my intention is to call the webservise WCF and through a JSON String in making the request to the server,
this interprets the request and return a response obviously.
Happens that the answer is always "Invalid Request"

What this WebService does:
The aim was webservice read the data structure in JSON and record the same in 2 tables in SQL

Below is the code implementation:
// --------------------------
Function Soap_912()
local cxml,canswer,nn1

// this sting in format JSON is only a test below is the struture of the JSON string

cJson := '{ "Tabelas":[{"Tabela":"TB0001StkDocCabecalho","Dados":[{"Campos":[{"Nome":"Ano","Valor":"2014"},{"Nome":"Codigo_Documento","Valor":"FT"},{"Nome":"Codigo_Serie","Valor":"2"},{"Nome":"Data","Valor":"2014-07-22 11:00:00"},{"Nome":"Mes","Valor":"7"},{"Nome":"Tipo_Entidade","Valor":"C"},{"Nome":"Codigo_entidade","Valor":"2897"},{"Nome":"Data_vencimento","Valor":"2014-08-22 00:00:00"},{"Nome":"Obs","Valor":"Os Serviços/ Produtos "},{"Nome":"Ultimo_Login","Valor":"andreia"}],"Dependencias":[{"Tabela":"TB0001StkDocLinhas","Dados":[{"Campos":[{"Nome":"Num_LinhaNoDoc","Valor":"1"},{"Nome":"Codigo_Movimento_Stock","Valor":"10"},{"Nome":"Codigo_Serv_Desp","Valor":"1"},{"Nome":"Codigo_Artigo","Valor":"NULL"},{"Nome":"Artigo","Valor":"AUTO DE MEDIÇÃO Nº 10, EM ANEXO"},{"Nome":"Perc_Desconto_Linha","Valor":"0"},{"Nome":"Qtd","Valor":"1.000"},{"Nome":"Ultimo_Login","Valor":"andreia"}],"Dependencias":[]}]}]}]}]}'

cxml := '<?xml version="1.0" encoding="utf-8"?>'+chr(13)+chr(10)
cxml += '<soap-env:envelope xmlns:ns1="http://tempuri.org/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelop ... 3)+chr(10)
cxml += '<soap-env:Body>'+chr(13)+chr(10)
cxml += '<ns1:GravaDocumentoStk>'+chr(13)+chr(10)
cxml += '<JSon_arg>'+ cJson + '</JSon_arg>'+chr(13)+chr(10)
cxml += '<strCodDocOrigem_arg>FT</strCodDocOrigem_arg>'+chr(13)+chr(10)
cxml += '<Empresa_arg>0001</Empresa_arg>'+chr(13)+chr(10)
cxml += '<Utilizador_arg>sa</Utilizador_arg>'+chr(13)+chr(10)
cxml += '<Password_arg>vazio</Password_arg>'+chr(13)+chr(10)
cxml += '</ns1:GravaDocumentoStk>'+chr(13)+chr(10)
cxml += '</soap-env:Body>'+chr(13)+chr(10)
cxml += '</soap-env:Envelope>'+chr(13)+chr(10)

canswer := fHttpExecute(cxml)
canswer := strtran(canswer,"><",">"+chr(10)+"<")

MSGINFO('Servido terminado')
return .t.

Function fHttpExecute(cxml)
local endpointUrl,curlHandle,curlErr
local aHeader,cc1

cc1 := ""

endpointUrl = "http://pc-srv-4.canasic1.pt/GexorWS/WCFAPIGexor.svc"

aHeader := {}

AADD(aHeader,"Content-Type: text/xml;charset=UTF-8" )
AADD(aHeader,'SOAPAction: "http://tempuri.org/IWCFAPIGexor/GravaDocumentoStk"' )

curlHandle := curl_easy_init()

if !empty(curlHandle)
curl_easy_setopt(curlHandle,HB_CURLOPT_HTTPHEADER,aHeader) /* Specify the Header data */

curl_easy_setopt(curlHandle, HB_CURLOPT_URL, endpointUrl) /* Set the endpoint to send the POST to */

curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD ) /* Setup response data */
curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP ) /* Setup response data */

curl_easy_setopt(curlHandle, HB_CURLOPT_POST, 1) /* Specify the POST data */
curl_easy_setopt(curlHandle, HB_CURLOPT_POSTFIELDS, cxml) /* Specify the POST data */

curlErr := curl_easy_perform(curlHandle) /* Do everything */

if empty(curlErr) /* Report any errors */
cc1 := curl_easy_dl_buff_get( curlHandle ) /* store response in variable */
ccErro := curl_easy_strerror(curlErr)
MSGINFO( "No handle")
if !empty(curlHandle)
curl_global_cleanup( curlHandle ) /* Clean-up libcurl */
MSGINFO("Erro Libcurl não vazia")
if empty(cc1)
MSGINFO("Erro : Resposta vazia")
return cc1

///-------------------- // -------------------------

The arguments of the webservice are:
<xs:element name="GravaDocumentoStk">
<xs:element minOccurs="0" name="JSon_arg" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="strCodDocOrigem_arg" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Empresa_arg" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Utilizador_arg" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Password_arg" nillable="true" type="xs:string" />

Json Structure:

{ "Tabelas" :
[{ "Tabela" : "TB0001StkDocCabecalho" ,
"Dados" :
[{ "Campos" :
[{ "Nome" : "Ano" ,"Valor" : "2014" },
{"Nome":"Data","Valor":"2014-07-22 11:00:00"},
{"Nome":"Data_vencimento","Valor":"2014-08-22 00:00:00"},
{"Nome":"Obs","Valor":"Os Serviços/ Produtos "},
{"Nome":"Artigo","Valor":"AUTO DE MEDIÇÃO Nº 10, EM ANEXO"},

// -------------------------

So I execute the program and the only response is 'INVALID REQUEST'
Considering that JSON string is correct and the user and passwor is correct also

The error must be how the SOAP request is being constructed. Noted that the WebService is WCF.

Any ideas ?

Thanks in advance
Carlos Reis
Best Regards
Carlos Reis, Portugal
39º 46' 47" N
08º 55' 45 W

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests