代理访问网页,拿到图片,文件最终指向地址

网页中会用到很多相对路径 ../等

 1 if (url.startsWith("../")) {
 2             String link = getRealLink(hostUrl.getPath());
 3             String[] str = hostUrl.getPath().split("/");
 4             int len = url.split("\\.\\./").length;
 5             String s = "";
 6             if (link.split("/").length != 3) {
 7                 for (int i = 0; i < str.length - len; i++) {
 8                     s += str[i] + "/";
 9                 }
10                 url = s + url.replaceAll("\\.\\./", "");
11             } else {
12                 url = link + "/" + url.replaceAll("\\.\\./", "");
13             }
14         }
 1 /**
 2      * 获得最终访问地址
 3      * 
 4      * @param link
 5      * @return
 6      * @throws ProtocolException
 7      */
 8     private String getRealLink(String link) {
 9         DefaultHttpClient httpClient = new DefaultHttpClient();
10         HttpGet httpget = new HttpGet(link);
11         HttpContext context = new BasicHttpContext();
12         HttpResponse response = null;
13         try {
14             response = httpClient.execute(httpget, context);
15         } catch (ClientProtocolException e1) {
16         } catch (IOException e1) {
17         }
18         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
19             try {
20                 throw new IOException(response.getStatusLine().toString());
21             } catch (IOException e) {
22             }
23         HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
24         HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
25         link = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq
26                 .getURI());
27         return link;
28     }

简单记录下!

来自:http://stackoverflow.com/questions/1456987/httpclient-4-how-to-capture-last-redirect-url

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值