uri,url和urn的区别以及URLEncoder
java.net.URL类不提供对标准RFC2396规定的特殊字符的转义,因此需要调用者自己对URL各组成部分进行encode。而java.net.URI则会提供转义功能。因此The recommended way to manage the encoding and decoding of URLs is to use java.net.URI. 可以使用URI.toURL()和URL.toURI()方法来对两个类型的对象互相转换。对于HTML FORM的url encode/decode可以使用java.net.URLEncoder和java.net.URLDecoder来完成,但是对URL对象不适用。
URLEncoder用法:有时请求的url中包含特殊字符,这时就需要调用UrlEncoder.encode方法对url请求参数进行加密,然后再作为参数传递就可以了。
例如:
http://api.wkinfo.com.cn/expert/law/article?indexId=law.legislation&title=《最高人民法院关于适用<中华人民共和国民事诉讼法>若干问题的意见》
http://api.wkinfo.com.cn/expert/law/article?indexId=law.legislation&title=《最高人民法院关于适用<中华人民共和国民事诉讼法>若干问题的意见》
HttpGet get = new HttpGet(url);
//HttpGet来自:org.apache.http.client.methods.HttpGet;
会出现以下异常信息:
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Illegal character in query at index 86: http://api.wkinfo.com.cn/expert/law/metadata?indexId=law.legislation&title=《最高人民法院关于适用<中华人民共和国民事诉讼法>若干问题的意见》
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.ilaw.regulation.service.impl.RegulationServiceImpl.getLawInfoListFromWordText(RegulationServiceImpl.java:51)
at com.ilaw.regulation.service.impl.RegulationServiceImplTest.getLawInfoListFromWordText(RegulationServiceImplTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 86: http://api.wkinfo.com.cn/expert/law/metadata?indexId=law.legislation&title=《最高人民法院关于适用<中华人民共和国民事诉讼法>若干问题的意见》
at java.net.URI.create(URI.java:852)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
at com.ilaw.regulation.util.WeiKeUtil.getHttpGet(WeiKeUtil.java:20)
at com.ilaw.regulation.util.WeiKeUtil.getLawInfo(WeiKeUtil.java:46)
at com.ilaw.regulation.util.RegulationThread.getContent(RegulationThread.java:57)
at com.ilaw.regulation.util.RegulationThread.call(RegulationThread.java:28)
at com.ilaw.regulation.util.RegulationThread.call(RegulationThread.java:17)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.URISyntaxException: Illegal character in query at index 86: http://api.wkinfo.com.cn/expert/law/metadata?indexId=law.legislation&title=《最高人民法院关于适用<中华人民共和国民事诉讼法>若干问题的意见》
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parseHierarchical(URI.java:3111)
at java.net.URI$Parser.parse(URI.java:3053)
at java.net.URI.<init>(URI.java:588)
at java.net.URI.create(URI.java:850)
... 10 more
这在浏览器中没有问题,但是使用apache的httpClient时就会抛出一个URI底层解析的错误,这时候就需要使用
String encodeLaw = URLEncoder.encode(law);
String url = ConfigParser.getMainUrl()+"expert/law/metadata?indexId=law.legislation&title="+encodeLaw; HttpGet get = new HttpGet(url);
URLEncoder 只对请求参数进行加密,然后传入请求就可以了。
这两天在写代码的时候,由于涉及到资源的位置,因此,需要在Java Bean中定义一些字段,用来表示资源的位置,比如:imgUrl,logoUri等等。但是,每次定义的时候,心里都很纠结,是该用imgUrl还是imgUri呢?
同样的,另外一个问题:String HttpServletRequest.getRequestURI();和StringBuffer HttpServletRequest.getRequestURL();返回的内容有何不同?为什么会如此?
带着这些问题到网上去搜了下,没发现让自己看了明白的解释,于是,想到了Java类库里有两个对应的类java.net.URI和java.net.URL,终于,在这两个类里的javadoc里找到了答案。
URIs, URLs, and URNs
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源。而URL是uniform resource locator,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。而URN,uniform resource name,统一资源命名,是通过名字来标识资源,比如mailto:java-net@java.sun.com。也就是说,URI是以一种抽象的,高层次概念定义统一资源标识,而URL和URN则是具体的资源标识的方式。URL和URN都是一种URI。
在Java的URI中,一个URI实例可以代表绝对的,也可以是相对的,只要它符合URI的语法规则。而URL类则不仅符合语义,还包含了定位该资源的信息,因此它不能是相对的,schema必须被指定。
ok,现在回答文章开头提出的问题,到底是imgUrl好呢,还是imgUri好?显然,如果说imgUri是肯定没问题的,因为即使它实际上是url,那它也是uri的一种。那么用imgUrl有没有问题呢?此时则要看它的可能取值,如果是绝对路径,能够定位的,那么用imgUrl是没问题的,而如果是相对路径,那还是不要用ImgUrl的好。总之,用imgUri是肯定没问题的,而用imgUrl则要视实际情况而定。
第二个,从HttpServletRequest的javadoc中可以看出,getRequestURI返回一个String,“the part of this request’s URL from the protocol name up to the query string in the first line of the HTTP request”,比如“POST /some/path.html?a=b HTTP/1.1”,则返回的值为”/some/path.html”。现在可以明白为什么是getRequestURI而不是getRequestURL了,因为此处返回的是相对的路径。而getRequestURL返回一个StringBuffer,“The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.”,完整的请求资源路径,不包括querystring。
总结一下:URL是一种具体的URI,它不仅唯一标识资源,而且还提供了定位该资源的信息。URI是一种语义上的抽象概念,可以是绝对的,也可以是相对的,而URL则必须提供足够的信息来定位,所以,是绝对的,而通常说的relative URL,则是针对另一个absolute URL,本质上还是绝对的。
注:这里的绝对(absolute)是指包含scheme,而相对(relative)则不包含scheme。
URI抽象结构 [scheme:]scheme-specific-part[#fragment]
[scheme:][//authority][path][?query][#fragment]
authority为[user-info@]host[:port]
参考资料:
http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URI.html
http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html
ps:
java.net.URL类不提供对标准RFC2396规定的特殊字符的转义,因此需要调用者自己对URL各组成部分进行encode。而java.net.URI则会提供转义功能。因此The recommended way to manage the encoding and decoding of URLs is to use java.net.URI. 可以使用URI.toURL()和URL.toURI()方法来对两个类型的对象互相转换。对于HTML FORM的url encode/decode可以使用java.net.URLEncoder和java.net.URLDecoder来完成,但是对URL对象不适用。
uri,url和urn的区别以及URLEncoder的更多相关文章
- [转载]URI、 URL 和 URN 的区别
1. URI URI = Universal Resource Identifier 统一资源标志符 URI采用一种特定语法标识一个资源的字符串.所标识的资源可能是服务器上的一个文件.不过,也可能是一 ...
- HTTP系列(一)URI、URL、URN的区别
1.URI.URL.URN关系图 1)URI Uniform Resource Identifier 统一资源标识符 每个web服务器资源都有一个名字,服务器资源名被统称为统一资源标识符:URI就像 ...
- URI、URL以及URN的区别
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...
- URI,URL与URN的区别
2017-11-13 16:51:49 URI = Universal Resource Identifier 统一资源标志符 URL = Universal Resource Locator 统一资 ...
- URI、URL 和 URN的区别
关于URI:URL类似于住址,它告诉你一种寻找目标的方式. URL和URN都是URI的子集. URI规范:协议名称://域名.根域名/目录/文件名.后缀#sddf URI :Unifor ...
- URI, URL, and URN
URI: uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. URL: uniform resource locator,统一资源定位器,它是一种具体的U ...
- (三)URI、URL和URN/GET与POST的区别
(一)URI.URL.URN HTTP使用统一资源标识符(Uniform Resource Identifiers,URI)来传输数据和建立连接. URL是一种特殊类型的URI,包含了用于查找某个资源 ...
- 关于URI URL URN
刚琢磨.整理了关于escape.encodeURIComponent.encodeURI的知识.突然又对URI有点模糊了,遂整理了以下资源 : 资源一: URL,URI 和URN 的举例理解 资源二: ...
- URL、URN、URI的区别?
URL.URN.URI区别 既然Web应用程序的文件等资源是放在服务器上,而服务器是因特网(Internet)上的主机,当然必须要有个方法,告诉浏览器到哪里取得文件等资源.通常会听到有人这么说:“你要 ...
随机推荐
- Python单元测试框架之pytest 2 -- 生成测试报告
From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...
- 关于Maven+Springmvc+Dubbo+Zookeeper整合
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat | - ...
- Importing multi-valued field into Solr from mySQL using Solr Data Import Handler
http://stackoverflow.com/questions/20233837/importing-multi-valued-field-into-solr-from-mysql-using- ...
- hadoop-n.x.y.tar.gz、hadoop-n.x.y.tar.gz.asc 、hadoop-n.x.y.tar.gz.md5 、hadoop-n.x.y.tar.gz.mds分别是什么?
不多说,直接上干货! 我这里,以hadoop-2.6.0为例. hadoop-n.x.y.tar.gz.mds,此mds文件是为了检验在下载和移动文件过程中文件的完整性. 通过验证文件的md5值去检验 ...
- ospf精确宣告地址
ospf的一点小问题 http://bbs.51cto.com/thread-881459-1.html 参照博客地址 network 172.20.1.0 0.0.0.3 area 0 networ ...
- linux删除文件夹下除了某一个文件之外的所有文件及find用法
原文: https://www.jb51.net/article/99319.htm 比如一个目录下有1,2,3,4,5这五个文件,现在我需要删除除了2以外的所有文件,那么我可以使用 find . ! ...
- android 根据滑动隐藏或显示导航 类似手机QQ好友个人信息
//重写ScrollView public class NotifyingScrollView extends ScrollView { /** * @author Cyril Mottier */ ...
- keras的LSTM函数详解
keras.layers.recurrent.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias= ...
- /storage/xx-xx/, /sdcard, /mnt/sdcard 三者的区别
本文针对Android 7.1 /sdcard是/mnt/sdcard的符号链,指向/storage/self/primary, /mnt/sdcard,也是符号链,指向/storage/self/p ...
- Java - 34 Java 文档注释
Java 文档注释 Java只是三种注释方式.前两种分别是// 和/* */,第三种被称作说明注释,它以/** 开始,以 */结束. 说明注释允许你在程序中嵌入关于程序的信息.你可以使用javadoc ...