HttpClient - Cookies - 和JEditorPane [英] HttpClient - Cookies - and JEditorPane

查看:125
本文介绍了HttpClient - Cookies - 和JEditorPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功设法使用httpclient登录网站并打印出启用该登录的Cookie。
但是,我现在卡住了,因为我想使用.setPage(url)函数在JEditorPane中显示后续页面。但是,当我这样做并使用Wireshark分析我的GET请求时,我发现用户代理不是我的httpclient,而是以下内容:

I've successfully managed to logon to a site using httpclient and print out the cookies that enable that logon. However, I am now stuck because I wanted to display subsequent pages in a JEditorPane using .setPage(url) function. However, when I do that and analyse my GET request using Wireshark I see that the user agent is not my httpclient but the following:

User-Agent:Java / 1.6。 0_17

User-Agent: Java/1.6.0_17

GET请求(在jeditorpane的setPage(URL url)方法的某处编码)没有使用httpclient检索的cookie。我的问题是 - 我怎么能以某种方式转移使用httpclient收到的cookie,以便我的JEditorPane可以显示来自网站的URL?
我开始认为这是不可能的,我应该尝试使用普通的Java URL连接等登录,但宁愿坚持使用httpclient,因为它更灵活(我认为)。大概我仍然会遇到cookie的问题??

The GET request (which is coded somewhere in side jeditorpane's setPage(URL url) method) does not have the cookies that were retrieved using the httpclient. My question is - how can I somehow transfer the cookies received with httpclient so that my JEditorPane can display URLs from the site? I'm beginning to think it's not possible and I should try and logon using normal Java URLconnection etc but would rather stick with httpclient as it's more flexible (I think). Presumably I would still have a problem with the cookies??

我曾想过扩展JEditorPane类并重写setPage()但我不知道实际的代码我应该放入它,因为似乎无法找出setPage()实际上是如何工作的。

I had thought of extending the JEditorPane class and overriding the setPage() but I don't know the actual code I should put in it as can't seem to find out how setPage() actually works.

任何帮助/建议将不胜感激。

Any help/suggestions would be greatly appreciated.

Dave

推荐答案

正如我在评论中提到的,HttpClient和JEditorPane使用的URLConnection获取URL内容不要互相交谈。因此,HttpClient可能获取的任何cookie都不会转移到URLConnection。但是,您可以像这样子类化JEditorPane:

As I mentioned in the comment, HttpClient and the URLConnection used by the JEditorPane to fetch the URL content don't talk to each other. So, any cookies that HttpClient may have fetched won't transfer over to the URLConnection. However, you can subclass JEditorPane like so :

final HttpClient httpClient = new DefaultHttpClient();

/* initialize httpClient and fetch your login page to get the cookies */

JEditorPane myPane = new JEditorPane() {
    protected InputStream getStream(URL url) throws IOException {

        HttpGet httpget = new HttpGet(url.toExternalForm());

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();

        // important!  by overriding getStream you're responsible for setting content type!
        setContentType(entity.getContentType().getValue());

        // another thing that you're now responsible for...  this will be used to resolve
        // the images and other relative references.  also beware whether it needs to be a url or string
        getDocument().putProperty(Document.StreamDescriptionProperty, url);

        // using commons-io here to take care of some of the more annoying aspects of InputStream
        InputStream content = entity.getContent();
        try {
            return new ByteArrayInputStream(IOUtils.toByteArray(content));
        }
        catch(RuntimeException e) {
            httpget.abort();  // per example in HttpClient, abort needs to be called on unexpected exceptions
            throw e;
        }
        finally {
            IOUtils.closeQuietly(content);
        }
    }
};

// now you can do this!
myPane.setPage(new URL("http://www.google.com/"));

通过进行此更改,您将使用HttpClient获取JEditorPane的URL内容。一定要在这里阅读JavaDoc http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JEditorPane.html#getStream(java.net.URL )以确保您抓住所有的角落情况。我想我的大部分都是排序的,但我不是专家。

By making this change, you'll be using HttpClient to fetch the URL content for your JEditorPane. Be sure to read the JavaDoc here http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JEditorPane.html#getStream(java.net.URL) to make sure that you catch all the corner cases. I think I've got most of them sorted, but I'm not an expert.

当然,你可以改变HttpClient部分避免首先将响应加载到内存中的代码,但这是最简洁的方法。因为你要将它加载到编辑器中,所以它在某些时候都会在内存中。;)

Of course, you can change around the HttpClient part of the code to avoid loading the response into memory first, but this is the most concise way. And since you're going to be loading it up into an editor, it will all be in memory at some point. ;)

这篇关于HttpClient - Cookies - 和JEditorPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆