此示例演示如何使用java发送http请求来登录o2server,获取登录认证xtoken信息
完整代码示例:
使用账号密码发送请求登录
package net.o2oa.demos; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.url; import java.net.urlconnection; import org.apache.commons.codec.binary.stringutils; import org.json.jsonobject; /** * 此示例演示如何使用java发送http请求来登录o2server,获取登录认证xtoken信息 * @author 九游会官网登录入口网页-ag8九游会j9登录入口 */ public class demo_logintoserver { static final string url_login = "/x_organization_assemble_authentication/jaxrs/authentication"; public static void main(string[] args) { string applicationserver = "127.0.0.1"; integer applicationport = 20020; string username = "张三"; string password = "o2oa@2022"; try { loginresult result = login(applicationserver, applicationport, username, password); if (stringutils.equals("success", result.gettype())) { system.out.println("xtoken=" result.gettoken()); } else { system.out.println("message:" result.getmessage()); } } catch (exception e) { e.printstacktrace(); } } //服务地址:http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/authentication // {"credential":"xadmin","password":"o2oa@2022"} /** * 使用登录认证的接口进行服务器登录,获取xtoken信息 * @param applicationserver 127.0.0.1 * @param applicationport 20020 * @param username 张三 * @param password o2oa@2022 * @return * @throws exception */ public static loginresult login(string applicationserver, integer applicationport, string username, string password) throws exception { //参数 string loginurl = "http://" applicationserver ":" applicationport url_login; string loginparams = string.format("{'credential':'%s','password':'%s'}", username, password); string responsedata = sendpost(loginurl, loginparams); jsonobject result = new jsonobject(responsedata); string type = result.getstring("type"); if (stringutils.equals("success", type)) { //登录成功 jsonobject data = result.getjsonobject("data"); string token = data.getstring("token"); return new loginresult("success", token, "登录成功!"); } else { //登录失败 return new loginresult("error", null, "用户不存在或者密码错误!"); } } /** * 发送post请求 * * @param url 地址 * @param param 传入的数据 * @return */ public static string sendpost(string url, string param) { printwriter out = null; bufferedreader in = null; string result = ""; try { url realurl = new ; urlconnection conn = realurl.openconnection(); conn.setrequestproperty("accept", "*/*"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("content-type", "application/json; charset=utf-8"); conn.setrequestproperty("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)"); conn.setdooutput(true); conn.setdoinput(true); out = new printwriter(conn.getoutputstream()); out.print(param); out.flush(); in = new bufferedreader(new inputstreamreader(conn.getinputstream())); string line; while ((line = in.readline()) != null) { result = line; } } catch (exception e) { e.printstacktrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (ioexception ex) { ex.printstacktrace(); } } return result; } }
loginresult.java
public static class loginresult { private string type; private string token; private string message; public loginresult(string type, string token, string message) { super(); this.type = type; this.token = token; this.message = message; } public string gettype() { return type; } public void settype(string type) { this.type = type; } public string gettoken() { return token; } public void settoken(string token) { this.token = token; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } }
成功响应结果json:
{ "type": "success", "data": { "tokentype": "manager", "token": "heeozivgpjqauhsaf5z5qkco-_-iwowweidiukmfurq", "rolelist": [ "manager", "organizationmanager", "meetingmanager", "processplatformmanager" ], "id": "xadmin", "name": "xadmin", "employee": "xadmin", "distinguishedname": "xadmin@o2oa@p", "mail": "xadmin@o2oa.net", "weixin": "", "qq": "", "mobile": "" }, "message": "", "date": "2019-10-19 14:15:17", "spent": 18, "size": -1, "count": 0, "position": 0 }
失败响应结果json:
{ "readystate": 4, "responsetext": "{"type": "error","message": "用户不存在或者密码错误.","date": "2019-10-19 14:34:34","spent": 9, "size": -1, "count": 0, "position": 0, "prompt":"com.x.organization.assemble.authentication.jaxrs.authentication.exceptionpersonnotexistorinvalidpassword" }", "responsejson": { "type": "error", "message": "用户不存在或者密码错误.", "date": "2019-10-19 14:34:34", "spent": 9, "size": -1, "count": 0, "position": 0, "prompt": "com.x.organization.assemble.authentication.jaxrs.authentication.exceptionpersonnotexistorinvalidpassword" }, "status": 500, "statustext": "internal server error" }
pom.xml:
4.0.0 net.o2oa.demos test_start_process_demo 0.0.1-snapshot jar test_start_process_demo http://maven.apache.org utf-8 org.apache.httpcomponents httpclient 4.5.10 org.apache.httpcomponents httpcore-nio 4.4.12 org.apache.httpcomponents httpcore 4.4.12 org.apache.httpcomponents httpmime 4.5.10 org.apache.commons commons-lang3 3.9 com.google.code.gson gson 2.8.5 org.json json 20190722 junit junit 3.8.1 test
若有收获,就点个赞吧