你如何以同样的方式做浏览器的URL相结合片段在Java中? [英] How do you combine URL fragments in Java the same way browsers do?

查看:86
本文介绍了你如何以同样的方式做浏览器的URL相结合片段在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意我要求现实世界的问题,不是理论的目的;看到问题的最后一部分 - 以同样的方式浏览器做的工作



通常你会看到答案:

NOTE: I am asking for real world problem, not for theoretical purpose; see the last part of the question -- the same way browsers do the job.


Usually you would see the answer:

new java.net.URL(new java.net.URL(base_url),rel_url).toString

BASE_URL rel_url 字符串)。在我的情况的 BASE_URL 是我拿来页面的URL, rel_url 来自< A HREF = ...的价值,所以它可能是单一的,甚至#字符(例如)

(base_url and rel_url are String). In my case base_url is the URL of page I fetched, rel_url comes from "<a href=..." value, so it might be even single "#" character (for example).

然而,这样的code没有对URL片段的工作,像这样两件:

However such code does not work for URL fragments, like such two pieces:

HTPP://www.hello.com/1.html

htpp://www.hello.com/1.html

P = 2

我测试火狐,铬,歌剧,Konqueror中,Web浏览器(侏儒谦虚;-D) - 所有这些结合这些网址:

I tested Firefox, Chromium, Opera, Konqueror, "Web Browser" (Gnome modesty ;-D) -- all of them combine those URLs as:

HTPP://www.hello.com/1.html P = 2

htpp://www.hello.com/1.html?p=2

使用code如上我得到:

With code as above I get:

HTPP://www.hello.com/ P = 2

htpp://www.hello.com/?p=2

如何结合网址片段,在准备世界的方式?

Question

How do you combine URL fragments, in a ready for world manner?

我希望有对于已经得心应手库,我才开始用自己做解析; - )

推荐答案

您误解的URL是什么。 P = 2 查询字符串的,而不是相对URL。 (您也可以找到 #foo ,这通常称为的片段标识符引用的且最常用的跳转到一个长文档的部分)。 URI的完整方案是许多在其他地方(你也可以找到在维基百科中描述 URI和在各种场所的URL)之间的差异。

You are misunderstanding what a URL is. ?p=2 is a query string, not a relative URL. (You may also find #foo, which is usually called a fragment identifier or reference and is most commonly used to jump to a section of a long document). The full scheme for URIs is described on Wikipedia among many other places (you can also find the differences between URIs and URLs in various places).

总之,相对URL指向的只路径的URL的一部分 - 它是路径是绝对的还是相对的。如果你有一个查询字符串,并希望将其附加到现有的URL(不具有查询字符串),只需将其添加到字符串。如果你不知道你是否有一个查询字符串,您可以使用URL类的方法来测试它。

Anyway, relative URLs refer only to the path part of the URL--it is whether the path is absolute or relative. If you have a query string and wish to attach it to an existing URL (which does not have a query string), just append it to the string. If you don't know whether you have a query string, you can use the methods in the URL class to test for it.

如果您要复制什么浏览器会给出一个完整的网址网​​址和字符串取值

If you want to replicate what browsers do, given a full URL url and a String s,

if (s.startsWith("?") || s.startsWith("#")) new java.net.URL(url.toString + s)
else new java.net.URL(url, s)

应该做的伎俩。 (我不知道确切的code,不同的浏览器使用,但这种复制你描述追加查询字符串的行为,如果这是所有在A HREF提供。)如果你不知道你做现有的URL可能有查询字符串或没有,那么你可以

should do the trick. (I don't know the exact code that different browsers use, but this replicates the behavior that you describe of appending a query string if that is all that is provided in a href.) If you don't know whether your existing URLs might have query strings or not, then you can

if (s.startsWith("#")) new java.net.URL(url.toString.takeWhile(_ != '#') + s)
else if (s.startsWith("?")) new java.net.URL(url.toString.takeWhile(_ != '?') + s)
else new java.net.URL(url, s)

这篇关于你如何以同样的方式做浏览器的URL相结合片段在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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