代码如下
@RequestMapping("/query")
@ResponseBody
public String getCount() {
String LOGINURL = "http://ip/workspace/index.jsp?framed=true";
String HTTPURL = "http://ip/HyperionPlanning/servlet/HspLogOn";
HttpClient http = null;
String userid = "admin";
String token = null;
String sessionId = null;
http = new DefaultHttpClient();
try {
String url = LOGINURL + "&username=" + userid;
HttpPost post = new HttpPost(url);
// post.setHeader("HYPLOGIN", userid);
HttpResponse response = http.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
String string = result.toString();
// System.out.println(string);
string = string.substring(0, 2500);
// System.out.println(string);
String regex = "sToken.*=.*\"(.+?)\";.*sCSRFToken";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(string);
while (matcher.find()) {
token = matcher.group(1);
}
post.abort();
// String mtoken = token.replace("\\n", "\\\n") + "=";
// System.out.println(mtoken);
HttpPost posthttp = new HttpPost(HTTPURL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("Application", "FYCWYS"));
nvps.add(new BasicNameValuePair("isContained", "true"));
nvps.add(new BasicNameValuePair("isWorkspace", "true"));
nvps.add(new BasicNameValuePair("LOCALE_LANGUAGE", "zh_CN"));
nvps.add(new BasicNameValuePair("moduleid", "HyperionPlanning.planning.7"));
nvps.add(new BasicNameValuePair("themeSelection", "BpmTadpole"));
nvps.add(new BasicNameValuePair("upk_available", "false"));
String mtoken = token.replace("\\n", "\\\n") + "=";
nvps.add(new BasicNameValuePair("sso_token", mtoken));
posthttp.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse responseSession = http.execute(posthttp);
posthttp.abort();
Header[] headers = responseSession.getHeaders("Set-Cookie");
for (Header header : headers) {
if (header.toString().contains("SESSIONID")) {
String str = header.toString();
// String reg = "Cookie:(.+?);.*path";
String reg = "JSESSIONID=(.+?);.*path";
Pattern pattern = Pattern.compile(reg);
Matcher m = pattern.matcher(str);
while (m.find()) {
sessionId = m.group(1).trim();
}
}
}
System.out.println(sessionId);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sessionId);
return sessionId;
}
启动tomcat 报错
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/client/ClientProtocolException但是在public static void main(String[] args) {
oaLoginLimitedServlet a=new oaLoginLimitedServlet();
System.out.println( "打印"+a.getCount());
}调用是正常的