我可以使用HttpClient获取缓存的图像吗?(Can I get cached images using HttpClient?)

编程入门 行业动态 更新时间:2024-10-13 04:21:44
可以使用HttpClient获取缓存的图像吗?(Can I get cached images using HttpClient?)

是否可以使用HttpClient加载登录页面,并从缓存中获取img元素的图像文件,而不是从src链接获取,而不重新加载? 这很重要,因为我需要为刚加载的页面保存验证码,如果我尝试从src链接加载它,它将是另一个验证码。 我试过了:

DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://www.mysite/login.jsp"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); OutputStream outstream = new FileOutputStream("d://file.html"); org.apache.commons.io.IOUtils.copy(instream, outstream); outstream.close(); instream.close();

但没有任何图像。 我也尝试过来自selenium库的HtmlUnitDriver ,也没有任何图像。 也许我必须尝试其他的东西? 你能帮帮我吗?

谢谢,抱歉我的英语。

Is it possible to load login page once, using HttpClient, and get image file of img element from cache, not from src link, without reload? It is important because I need to save captcha for just loaded page, if I try load it from src link, it will be another captcha. I tried:

DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://www.mysite/login.jsp"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); OutputStream outstream = new FileOutputStream("d://file.html"); org.apache.commons.io.IOUtils.copy(instream, outstream); outstream.close(); instream.close();

but there are not any images. I also tried HtmlUnitDriver from selenium library, there are not any images too. Maybe I must try something else? Can you help me with it?

Thanks and sorry for my English.

最满意答案

正如它在这里提到的: HttpClient从响应中获取图像 DefaultHttpClient / HttpClient得到的只有一个内容,在你的情况下它是一个HTML页面(服务于: http://www.mysite/login.jsp )。 你需要解析那个HTML页面并获得指定的img标签,而不是你只需要下载它(只需要重新发送login.jsp请求!)。 如果您下载验证码图像,则需要尽快获取该图像,否则可能会被试图登录的其他用户覆盖。

正如浏览器所做的那样,你需要以同样的方式,下载HTML,而不是解析它,而不是请求所有src / link / ect取决于你需要什么。

DefaultHttpClient默认不缓存。

默认情况下启用CachingHttpClient缓存,在这种情况下,您需要分析If-Modified-Since和If-None-Match标头,以确定是否执行了对远程服务器的请求,或者是否从缓存返回了结果。 如果服务器上没有任何更改,您将获得缓存数据(如果先前已缓存)。

As it mentioned here: HttpClient Get images from response the DefaultHttpClient/HttpClient get's only one content, which is in your case it's an HTML page (served from: http://www.mysite/login.jsp). Than you need to parse that HTML page and get the specified img tag with it's src than you need only to download it (ONLY that, without resend the login.jsp request!). If you download a captcha image you need to get that image as soon as possible or it could be overwritten by another user, who tries to login.

As the browser does, you need to do the same way, download HTML, than parse it, than request all src/link/ect depends on what you need.

DefaultHttpClient doesn't cache by default.

CachingHttpClient cache is enabled by default, in this case you need to analyzes If-Modified-Since and If-None-Match headers in order to decide if request to the remote server is performed, or if its result is returned from cache. If there's no change on the server, you will get cached data, if you cached previously.

更多推荐

本文发布于:2023-08-05 15:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   缓存   图像   images   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!