Call Rest Service Using Authen In PowerBuilder . Using input json data and response json data type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
//==================================================================== // Function: f_response() //-------------------------------------------------------------------- // Description: REST service In PowerBuilder With Authen //-------------------------------------------------------------------- // Arguments: // String as_url: Url Send Data // String as_json_input: input data // Ref String as_json_output: input data // Boolean ab_authen: Boolean Flag used authen webservices // String as_user: String User Authen // String as_passwd: String Passwords Authen //-------------------------------------------------------------------- // Returns: integer 0 success 1 error //==================================================================== OleObject loo_xmlhttp,lole_xml_document String ls_response_text,ls_status_text,ls_error_code,ls_error_msg, ls_error_message Long ll_status_code Int li_rc //send webservice Try //Create an instance of our COM object loo_xmlhttp = Create OleObject lole_xml_document = Create OleObject li_rc = loo_xmlhttp.ConnectToNewObject("MSXML2.ServerXMLHTTP.6.0") If li_rc = 0 Then //connect succeeds //Call Method POST If ab_authen Then //using authen loo_xmlhttp.Open ("POST",as_url, False, as_user, as_passwd) Else loo_xmlhttp.Open ("POST",as_url, False) End If loo_xmlhttp.SetRequestHeader( "Content-Type", "application/json; charset=utf-8") //content type json loo_xmlhttp.SetRequestHeader( "Accept", "application/json;") loo_xmlhttp.setOption(2,'13056') //remove check for server certificate revocation loo_xmlhttp.Send(as_json_input) //Send data //Get our response ls_status_text = loo_xmlhttp.StatusText ll_status_code = loo_xmlhttp.Status //Check HTTP Response code for errors If ll_status_code >= 300 Then lole_xml_document.DisconnectObject() loo_xmlhttp.DisconnectObject() If IsNull(ls_response_text) Then ls_response_text = "" End If MessageBox("Warning",String(ll_status_code) + '-' + ls_status_text + ls_response_text) Return 0 Else //Get the response we received from the web server ls_response_text = loo_xmlhttp.ResponseText lole_xml_document.DisconnectObject() loo_xmlhttp.DisconnectObject() as_json_output = ls_response_text End If End If Catch (RuntimeError rte) MessageBox("Warning",rte.getMessage()) loo_xmlhttp.DisconnectObject() Return 0 End Try Return 1 |
Good Luck!
Buen dÃa, una consulta
Donde obtiene el dato esta variable ls_response_text …?
If IsNull(ls_response_text) Then
ls_response_text = “”
End If
En mi aplicación tengo error en esta linea
loo_xmlhttp.status=200
Error accessing external object property responsetext at line 79
que podria ser
line 79? This function is current 70 line?
line 79 = If http.status=200 Then
Error Error accessing external object property responsetext at line 79
did you destroy objects? check if isavlid(loo_xmlhttp) then
ok, check if isavlid(loo_xmlhttp) then.
thanks.
———————————————
Here my code
is something wrong?
———————————————-
Error accessing external object property responsetext
If http.status=200 Then
ls_respuesta = http.responseText
———————————————————————-
Try
OLEObject http
http = Create OLEObject
li_create = http.ConnectToNewObject(“MSXML2.ServerXMLHTTP”)
If li_create <> 0 Then
li_create = http.ConnectToNewObject( “Msxml2.XMLHTTP.3.0”)
End If
If(li_create <> 0 ) then
ls_respuesta = “Msxml2 Component initialization failed!”
End if
boolean lb_result
OleObject dom
long result
dom = Create OleObject
result = dom.ConnectToNewObject(“Msxml2.DOMDocument.3.0″)
lb_result = dom.LoadXML (ls_xml)
IF lb_result = false THEN
destroy (dom)
END IF
ll_length = Len(ls_xml)
String sSoapAction
as_approve=”;
http.Open(“POST”,gs_urlws,True)
http.setRequestHeader(“Content-Type”,”text/xml;charset=UTF-8″)
http.setRequestHeader(“Content-Length”,String(ll_length))
http.setRequestHeader (“SOAPAction”,”http://tempuri.org/SendCPE”)
http.setTimeouts(5000,6000,10000,10000)
http.Send(dom.xml)
Do While http.readyState <> 4
Yield()
SetRedraw(true)
If http.readyState<> 4 Then
w=w + 1
If w > 10000 then
If isvalid(w_envio_fecredito_xml) Then close(w_envio_fecredito_xml)
exit
End If
End If
Loop
If http.status=200 Then
ls_respuesta = http.responseText
else
ls_respuesta = “”
End If
dom.DisconnectObject()
http.DisconnectObject()
Destroy (dom)
Destroy (http)
Catch(Exception ex)
ls_xml=””;
ls_respuesta = “RuntimeErrorExp: “+ex.GetMessage() +’-Invoca ws: ‘+string(ex.text)
FileClose (li_fichero)
If IsValid (http) then Destroy http;
If IsValid (dom) then Destroy dom;
Catch (RuntimeError err)
ls_xml=””;
ls_respuesta = “RuntimeError: “+err.GetMessage() +’-Invoca ws: ‘+string(err.text)
FileClose (li_fichero)
If IsValid (http) then Destroy http; //Destroy http
If IsValid (dom) then Destroy dom; //Destroy http
End Try
check if the problem is as follows
http.Open(“POST”,gs_urlws,True) change http.Open(“POST”,gs_urlws,False)
//http.setTimeouts(5000,6000,10000,10000) close line
check invalid If IsValid (http) Then and If IsValid (http.status) Then before If http.status = 200 Then
I think it’s because services haven’t returned.
Thank you
i tell you
Hello,
is there a way to get bearer authentication using that example of code? Tried it with no luck. I have PB 10.2
I would very much appreciate a response
Hello,
is there a way to implement bearer authentication with this code?
I have tried it with no luck. I’m using PB 10.2.
Any help appreciated
Thank you