Sunday 15 June 2014

java - Bonita Web API - 401 Unauthorized Error -



java - Bonita Web API - 401 Unauthorized Error -

i trying utilize bonita web api. code below. can see phone call loginservice before calling other api service. logs in ok 200. when create subsequent phone call list of processes 401 error. jsessionid first phone call , suppose pass subsequent calls authenticate you.

var baseaddress = new uri(<base address>); var cookiecontainer = new cookiecontainer(); using (var handler = new httpclienthandler() { cookiecontainer = cookiecontainer }) using (var client = new httpclient(handler) { baseaddress = baseaddress }) { httpresponsemessage result = client.postasync("/bonita/loginservice", new stringcontent("login=<username>,password=<password>,redirect=false")).result; client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage result2 = client.getasync("/bonita/api/bpm/process").result; result2.ensuresuccessstatuscode(); }

this works .net 2.0 c# has interesting things check.

webclient wc = new webclient(); wc.proxy = webrequest.getsystemwebproxy(); //wc.headers[httprequestheader.acceptencoding] = "gzip, deflate"; string strlogin = wc.downloadstring("http://localhost:8080/bonita/loginservice?username=walter.bates&password=bpm&redirect=false"); wc.headers[httprequestheader.cookie] = wc.responseheaders[httpresponseheader.setcookie].tostring(); string strcookie = wc.responseheaders[httpresponseheader.setcookie].tostring(); string strprocesses = wc.downloadstring("http://localhost:8080/bonita/api/bpm/process?p=0");

first of should know how determine executed operation successful ( login, getprocesses , whatever) when seek login header (for illustration "jsessionid=50e509d37ac28e2d725cbd45a8112fa7; path=/bonita; httponly") , ok 200 if login effort in bonita unsuccesful.

for successful login on previous example

1) must pass mandatory form data: username, password , redirect must sure pass redirect in lower case ."false" not work, "false" work. .net suppose have property-> boolean redirect. must create lowercase redirect.tostring().tolower() cause either way value "false" , don't want that.

let's seek login username , password without passing redirect. result both ok 200 , header response wrong (the response must empty), on next request (i.e getprocesses) you'll (401) unauthorized. guess results have if pass redirect=false instead of redirect=false. same.

2)you must get: strlogin="" // body of response must empty strcookie="jsessionid=4f67f134840a2c72dbb968d53772fb22; path=/bonita; httponly"

for successful getprocesses on previous illustration pass header got login

wc.headers[httprequestheader.cookie] = wc.responseheaders[httpresponseheader.setcookie].tostring();

and phone call process , string in json format illustration

"[{\"id\":\"6996906669894804403\",\"icon\":\"\",\"displaydescription\":\"\",\"deploymentdate\":\"2014-11-19 17:57:40.893\",\"description\":\"\",\"activationstate\":\"enabled\",\"name\":\"travel request\",\"deployedby\":\"22\",\"displayname\":\"travel request\",\"actorinitiatorid\":\"4\",\"last_update_date\":\"2014-11-19 17:57:41.753\",\"configurationstate\":\"resolved\",\"version\":\"1.0\"}]"

(or [] means empty json)

if cookie not passed correctly 1 time again 401 error.

solution .net 4.5.1

using system; using system.collections.generic; using system.diagnostics; using system.io; using system.linq; using system.net; using system.net.http; using system.net.http.headers; using system.text; using system.threading.tasks; using system.web; namespace bonitarestapi { class bonitaapi { private cookiecollection collection; string strcookietopass; string sessionid; static void main(string[] args) { bonitaapi obj = new bonitaapi(); task login = new task(obj.login); login.start(); login.wait(); console.readline(); task getprocesses = new task(obj.getprocesses); getprocesses.start(); getprocesses.wait(); console.readline(); task logout = new task(obj.logout); logout.start(); logout.wait(); console.readline(); } public async void login() { const string url = "http://localhost:8080/bonita/"; var cookies = new cookiecontainer(); var handler = new httpclienthandler(); handler.cookiecontainer = cookies; using (var client = new httpclient(handler)) { var uri = new uri(url); client.baseaddress = uri; //client.defaultrequestheaders.accept.clear(); //client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); var content = new formurlencodedcontent(new[] { new keyvaluepair<string, string>("username", "helen.kelly"), new keyvaluepair<string, string>("password", "bpm"), new keyvaluepair<string, string>("redirect", "false"), new keyvaluepair<string, string>("redirecturl", ""), }); httpresponsemessage response = await client.postasync("loginservice", content); if (response.issuccessstatuscode) { var responsebodyastext = await response.content.readasstringasync(); if (!string.isnullorempty(responsebodyastext)) { console.writeline("unsuccessful login.bonita bundle may not have been started, or url invalid."); return; } collection= cookies.getcookies(uri); strcookietopass = response.headers.getvalues("set-cookie").firstordefault(); sessionid = collection["jsessionid"].tostring(); console.writeline(string.format("successful login retrieved session id {0}", sessionid)); // useful work } else { console.writeline("login error" + (int)response.statuscode + "," + response.reasonphrase); } } } public async void logout() { const string url = "http://localhost:8080/bonita/"; var cookies = new cookiecontainer(); var handler = new httpclienthandler(); handler.cookiecontainer = cookies; using (var client = new httpclient(handler)) { var uri = new uri(url); client.baseaddress = uri; var content = new formurlencodedcontent(new[] { new keyvaluepair<string, string>("redirect", "false") }); httpresponsemessage response = await client.postasync("logoutservice", content); if (response.issuccessstatuscode) { var responsebodytext = await response.content.readasstringasync(); if (!string.isnullorempty(responsebodytext)) { console.writeline("unsuccessful logout.bonita bundle may not have been started, or url invalid."); return; } console.writeline("successfully logged out."); } else { console.writeline("logout error" + (int)response.statuscode + "," + response.reasonphrase); } } } public async void getprocesses() { var handler = new httpclienthandler(); cookie ok = new cookie("set-cookie:",strcookietopass); handler.cookiecontainer.add(collection); using (var client = new httpclient(handler)) { var builder = new uribuilder("http://localhost/bonita/api/bpm/process"); builder.port = 8080; var query = httputility.parsequerystring(builder.query); query["p"] = "0"; query["c"] = "10"; builder.query = query.tostring(); uri uri= new uri(builder.tostring()); client.baseaddress = uri; httpresponsemessage response = await client.getasync(uri.tostring()); if (response.issuccessstatuscode) { var responsebodytext = await response.content.readasstringasync(); if (string.isnullorempty(responsebodytext)) { console.writeline("unsuccessful getprocesses.bonita bundle may not have been started, or url invalid."); return; } console.writeline("successfully getprocesses:" + responsebodytext); } else { console.writeline("getprocesses error" + (int)response.statuscode + "," + response.reasonphrase); } } } } }

java web-services tomcat c#-4.0 bonita

No comments:

Post a Comment