通过Java内嵌浏览器登录http://container.open.taobao.com/container?appkey={ appkey } 授权后会在页面上得到一个字符串。当然通过神马httpclient啊之类的抓取页面源码都是抓取不到的。只有通过swt的不知道神马版本里面有一个browser.getText();才能得到源代码。之后把得到的字符串解码就OK了。附代码
package com.evangelsoft.jx.taobao.frame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;
import javax.swing.JInternalFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.evangelsoft.jx.taobao.api.config.TbConstan;
import com.evangelsoft.jx.taobao.api.config.TbExitLogo;
/**
* <p>Title: 获取top_session</p>
* <p>Description:内嵌获取top_session值的网页 </p>
* @date 2011-9-21 上午10:34:22
*/
@SuppressWarnings("serial")
public class GetTopSessionFrame extends JInternalFrame {
public String top_session = "";
/**
* 引用包资源文件。
*/
private static ResourceBundle res = ResourceBundle
.getBundle(GetTopSessionFrame.class.getPackage().getName() + ".Res");
public GetTopSessionFrame(boolean logooff) {
super();
try {
run(logooff);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
pack();
}
public GetTopSessionFrame() {
}
public void run(boolean logooff) throws UnsupportedEncodingException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText(res.getString("BROWSER_TITLE"));
createContents(shell, logooff);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window's contents
*
* @param shell
* the main window
* @throws UnsupportedEncodingException
*/
private void createContents(final Shell shell, final boolean logooff)
throws UnsupportedEncodingException {
shell.setLayout(new FormLayout());
shell.setSize(new Point(560, 614));
Point frameSize = shell.getSize();//获得窗体的大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕的大小
shell.setLocation((screenSize.width - frameSize.x) / 2,
(screenSize.height - frameSize.y) / 2);//居中
Composite controls = new Composite(shell, SWT.NONE);
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
controls.setLayoutData(data);
final Browser browser = new Browser(shell, SWT.FILL);
data = new FormData();
data.top = new FormAttachment(controls);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
browser.setLayoutData(data);
if (logooff) {
TbExitLogo logo = new TbExitLogo();
browser.setUrl(logo.getlogoffURL());
// GetTopSessionFrame f = new GetTopSessionFrame(logooff);
// f.setDefaultCloseOperation(GetTopSessionFrame.DO_NOTHING_ON_CLOSE);
} else {
browser.setUrl(TbConstan.TOP_SESSION_URL);
}
controls.setLayout(new GridLayout(6, false));
browser.addProgressListener(new ProgressListener() {
@Override
public void changed(ProgressEvent arg0) {
}
@Override
public void completed(ProgressEvent arg0) {
String url = browser.getUrl();
try {
if (("http://container.api.taobao.com/container")
.equals(url)) {
String container = browser.getText();
String[] arr = container.split("&");
for (int i = 0; i < arr.length; i++) {
if (arr[i].indexOf("top_session=") != -1) {
top_session = arr[i]
.replace("top_session=", "");
}
}
if (!("").equals(top_session)) {
TbConstan.TOP_SESSION = top_session;
shell.close();
}
}
if (("http://www.taobao.com/").equals(url)) {
shell.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}