citrix API 调用案例

来源:互联网 发布:sql模糊查询通配符 编辑:程序博客网 时间:2024/04/29 23:58

Citrix API 成功调用案例, 如有疑问请联系 QQ 232600624

package cn.ccvnc.http.service;import io.lions.http.client.ResultEntity;import io.lions.http.client.Transfer;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.impl.client.BasicCookieStore;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;/** * * @author ccvnc * */public class Citrix {static BasicCookieStore cookieStore = null;static CloseableHttpClient httpClient = null;static Map<String, String> cookieMap = new HashMap<String, String>();static HttpClientContext context = null;private String host;private int port = 80;public Map<String, String> basicHeaders() {String h = host;if (port > -1 && port != 80) {h += ":" + port;}Map<String, String> headers = new HashMap<String, String>();headers.put("Host", host);headers.put("Accept", "application/xml, text/xml, */*; q=0.01");headers.put("Accept-Language", "en-us,en;q=0.7,fr;q=0.3");headers.put("Accept-Encoding", "gzip, deflate");headers.put("X-Requested-With", "XMLHttpRequest");headers.put("X-Citrix-IsUsingHTTPS", "No");if (cookieMap != null) {headers.put("Csrf-Token", cookieMap.get("CsrfToken"));}headers.put("Referer", "http://" + h + "/Citrix/StoreWeb/");headers.put("Connection", "keep-alive");headers.put("Pragma", "no-cache");headers.put("Cache-Control", "no-cache");return headers;}public void context() {context = HttpClientContext.create();}// 1. /Home/Configurationpublic ResultEntity homeConfiguration() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/Home/Configuration");Map<String, String> headers = basicHeaders();String cookieString = Transfer.getInstance().cookieString(cookieMap, null);if (cookieString != null) {headers.put("Cookie", cookieString);}ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, null);cookieMap.putAll(result.getCookies());return result;}// 2. /Authentication/GetAuthMethodspublic ResultEntity authenticationGetAuthMethods() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/Authentication/GetAuthMethods");Map<String, String> headers = basicHeaders();String cookieString = Transfer.getInstance().cookieString(cookieMap, null);if (cookieString != null) {headers.put("Cookie", cookieString);}ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, null);cookieMap.putAll(result.getCookies());return result;}// 3.1 /ExplicitAuth/Loginpublic ResultEntity explicitAuthLogin() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/ExplicitAuth/Login");Map<String, String> headers = basicHeaders();headers.put("Cookie", Transfer.getInstance().cookieString(cookieMap, "CtxsAuthMethod=ExplicitForms;"));Map<String, String> params = new HashMap<String, String>();params.put("username", "");params.put("password", "");params.put("loginBtn", "登录");params.put("saveCredentials", "false");params.put("StateContext", "");ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "login", null, headers, params);cookieMap.putAll(result.getCookies());return result;}// 3.2 /ExplicitAuth/LoginAttemptpublic ResultEntity explicitAuthLoginAttempt() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/ExplicitAuth/LoginAttempt");Map<String, String> headers = basicHeaders();headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");headers.put("Cookie", Transfer.getInstance().cookieString(cookieMap, "CtxsAuthMethod=ExplicitForms;"));Map<String, String> params = new HashMap<String, String>();params.put("username", "");params.put("password", "");params.put("loginBtn", "登录");params.put("saveCredentials", "false");params.put("StateContext", "");ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, params);cookieMap.putAll(result.getCookies());return result;}// 4. /Resources/Listpublic ResultEntity resourcesList() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/Resources/List");Map<String, String> headers = basicHeaders();headers.put("Accept", "application/json, text/javascript, */*; q=0.01");headers.put("Content-Type", "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");Map<String, String> params = new HashMap<String, String>();params = new HashMap<String, String>();params.put("format", "json");params.put("resourcesDetails", "Default");ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, params);cookieMap.putAll(result.getCookies());return result;}// 5. /Authentication/GetUserNamepublic ResultEntity authenticationGetUserName() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/Authentication/GetUserName");Map<String, String> headers = basicHeaders();headers.put("Accept", "text/plain, */*; q=0.01");String cookieString = Transfer.getInstance().cookieString(cookieMap, null);if (cookieString != null) {headers.put("Cookie", cookieString);}ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, null);cookieMap.putAll(result.getCookies());return result;}// 6. /Resources/GetLaunchStatuspublic ResultEntity resourcesGetLaunchStatus(String id) throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/Resources/GetLaunchStatus/" + id);Map<String, String> headers = basicHeaders();String cookieString = Transfer.getInstance().cookieString(cookieMap, null);if (cookieString != null) {headers.put("Cookie", cookieString);}headers.put("Accept", "application/json, text/javascript, */*; q=0.01");Map<String, String> params = new HashMap<String, String>();params.put("displayNameDesktopTitle", "Desktop");ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "post", null, headers, params);cookieMap.putAll(result.getCookies());return result;}// 7. /Resources/LaunchIcapublic ResultEntity resourcesLaunchIca(String ica, String launchId) throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb/" + ica);Map<String, String> headers = basicHeaders();String cookieString = Transfer.getInstance().cookieString(cookieMap, null);if (cookieString != null) {headers.put("Cookie", cookieString);}headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");Map<String, String> params = new HashMap<String, String>();if (cookieMap != null) {params.put("CsrfToken", cookieMap.get("CsrfToken"));}params.put("launchId", launchId);params.put("displayNameDesktopTitle", "Desktop");ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "get", null, headers, params);cookieMap.putAll(result.getCookies());return result;}// 8.public ResultEntity citrixStoreWeb() throws MalformedURLException {URL url = Transfer.getInstance().url(host, port, "/Citrix/StoreWeb");Map<String, String> headers = basicHeaders();ResultEntity result = Transfer.getInstance().normal(httpClient, context, cookieStore, url, "get", null, headers, null);cookieMap.putAll(result.getCookies());return result;}public void execute() throws IOException {try {cookieStore = new BasicCookieStore();httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();homeConfiguration();context();authenticationGetAuthMethods();explicitAuthLogin();explicitAuthLoginAttempt();ResultEntity result_04 = resourcesList();JSONObject json = JSONObject.fromObject(result_04.getContent());JSONArray resources = (JSONArray) json.get("resources");if (resources != null) {for (int i = 0; i < resources.size(); i++) {JSONObject resource = resources.getJSONObject(i);String id = resource.getString("id");ResultEntity result_06 = resourcesGetLaunchStatus(id);JSONObject status = JSONObject.fromObject(result_06.getContent());if (status.getString("status").equalsIgnoreCase("success")) {String ica = resource.getString("launchurl");String launchId = resource.getString("id");ResultEntity result_07 = resourcesLaunchIca(ica, launchId);System.out.println(result_07.getContent());break;}}}} catch (Exception e) {e.printStackTrace();} finally {if (httpClient != null) {httpClient.close();}}}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public int getPort() {return port;}public void setPort(int port) {this.port = port;}public static void main(String[] args) throws Exception {Citrix client = new Citrix();client.execute();}}


0 0
原创粉丝点击