栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

HttpClient-Cookies-和JEditorPane

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpClient-Cookies-和JEditorPane

正如我在评论中提到的那样,HttpClient和JEditorPane用于获取URL内容的URLConnection彼此不对话。因此,HttpClient可能已获取的任何cookie都不会转移到URLConnection。但是,您可以像这样子类化JEditorPane:

final HttpClient httpClient = new DefaultHttpClient();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内容。请确保在此处http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JEditorPane.html#getStream(java.net.URL)阅读JavaDoc
,以确保您捕获了所有JavaDoc 角落里的情况。我想我已经对它们中的大多数进行了排序,但是我不是专家。

当然,您可以在代码的HttpClient部分周围进行更改,以避免首先将响应加载到内存中,但这是最简洁的方法。而且由于您要将其加载到编辑器中,因此某个时刻它们都将保存在内存中。;)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号