详见:
http://msnvip.iteye.com/blog/261159
公司使用单点登陆的时候, 需要在程序中提交用户名,密码到SSO服务器,并且返回ticket的数据信息。来实现登陆。
并且返回给IM系统。
其实这个和ASP里面的抓取数据的代码也比较像。
JAVA中,使用 HttpClient 类来实现这个功能。
首先要导入几个包:
commons-logging-1.1.1-bin
commons-codec-1.3.jar
commons-httpclient-3.1
可以在附件下载工程。
在程序中设置好地址, 端口,就可以访问返回的数据了 。
比较简便的代码:
String associationListUrl=BackendConstants.CLUB_CREATE_URL; //associationListUrl = http://club.woyo.com/api/club-creat.action HttpClient client = new HttpClient(); PostMethod method = new PostMethod(associationListUrl); method.addParameter("blockId",33005+""); //频道子板块ID method.addParameter("categoryId",14+""); //社团通用分类ID try { client.executeMethod(method); } catch (HttpException e) { throw new Exception("创建商会失败!连接失败: " + e.getMessage()); } catch (IOException e) { throw new Exception("创建商会失败!连接失败: " + e.getMessage()); } String returnJson = ""; try { returnJson = method.getResponseBodyAsString();; System.out.println("------------------getCreateClub--------------------returnStr--" + returnJson); String strJson = returnJson.replace("?(", "").replace(")", "").replace(";", ""); System.out.println("--------------------------------------strJson--" + strJson); } catch (IOException e) { throw new Exception("创建商会失败!" + e.getMessage()); }finally{ //使用完成后要释放链接 method.releaseConnection(); }
/* * Created on 2003-12-7 by Liudong */ package http.demo; import java.io.IOException; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; /** * 提交参数演示 * 该程序连接到一个用于查询手机号码所属地的页面 * 以便查询号码段1330227所在的省份以及城市 * @author Liudong */ public class SimpleHttpClient { public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http"); HttpMethod method = getPostMethod();//使用POST方式提交数据 client.executeMethod(method); //打印服务器返回的状态 System.out.println(method.getStatusLine()); //打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); //打印返回的信息 System.out.println(response); method.releaseConnection(); } /** * 使用GET方式提交数据 * @return */ private static HttpMethod getGetMethod(){ return new GetMethod("/simcard.php?simcard=1330227"); } /** * 使用POST方式提交数据 * @return */ private static HttpMethod getPostMethod(){ PostMethod post = new PostMethod("/simcard.php"); NameValuePair simcard = new NameValuePair("simcard","1330227"); post.setRequestBody(new NameValuePair[] { simcard}); return post; } }