HttpClient 学习记录---URIBuilder
来源: httpClient学习记录--URIBuilder
构造函数
URIBuilder()
URIBuilder(final String string) 内部会创建URI对象
URIBuilder(final URI uri)
非空的两个构造实际内部都调用了digestURI(uri) 将URI对象解析并赋值给类的属性
URIBuilder
正是采用了至少有7个参数,如果要满足各种需求的组合,恐怕总共要提供∑(C^7^~i~)(i=1~7)种构造函数,显然不现实。然而,URIBuilder
既可以造出一个不可变
的URI,又可以兼顾N种参数。URIBuilder
可以这样用:
1 2 3 4 5 6 7 8 9 10 | / http: //www.google.com/search?q=编程狗的博客&btnG=Google+Search&aq=f&oq= URI uri = new URIBuilder() .setScheme( "http" ) .setHost( "www.google.com" ) .setPath( "/search" ) .setParameter( "q" , "编程狗的博客" ) .setParameter( "btnG" , "Google Search" ) .setParameter( "aq" , "f" ) .setParameter( "oq" , "" ) .build(); |
uilder Pattern(建造者模式)。等号右边实际上是一行,先创建一个URIBuilder
对象实例,调用实例的setScheme方法,此方法顺便返回URIBuilder
对象实例,刚刚返回的这个实例调用setHost方法,...,最后一个返回的URIBuilder
对象实例调用build方法,返回URI对象。它是如何实现的呢?
原来的URI类的set方法的基础上,添加一个返回值,返回URIBuilder
自己就够了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class URIBuilder{ public URIBuilder setScheme(String scheme){ ... return this ; } public URIBuilder setHost(String host){ ... return this ; } public URIBuilder setPath(String path){ ... return this ; } //built 方法,把参数拼接,然后返回一个URI类 public URI built(){ ... return uri; } } |
由于URIBuilder
每次都返回它自己,所以可以连续的执行 set方法,最后通过built方法返回URI类。
类属性
String url = "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=";
URIBuilder uriBuilder = new URIBuilder(url);
System.out.println(uriBuilder.getScheme());
System.out.println(uriBuilder.getUserInfo());
System.out.println(uriBuilder.getHost());
System.out.println(uriBuilder.getPort());
System.out.println(uriBuilder.getPath());
System.out.println(uriBuilder.getQueryParams());
System.out.println(uriBuilder.getFragment());
System.out.println(uriBuilder.getCharset());
输出如下所示:
scheme:http
userinfo:null
host:www.google.com
prot:-1 端口默认是80,当显示指定端口时,此处便能取到值
path:/search
queryParams:[hl=en, q=httpclient, btnG=Google Search, aq=f, oq=]
fragment:null
charset:null
某些属性含义未知手动设置一次
1 | String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909" ;URIBuilder uriBuilder = new URIBuilder(url);uriBuilder.setFragment( "111" );uriBuilder.setUserInfo( "222" , "333" );uriBuilder.setCharset( new GBK());System.out.println(uriBuilder.build());System.out.println(uriBuilder.getScheme());System.out.println(uriBuilder.getUserInfo());System.out.println(uriBuilder.getHost());System.out.println(uriBuilder.getPath());System.out.println(uriBuilder.getQueryParams());System.out.println(uriBuilder.getFragment());System.out.println(uriBuilder.getCharset()); |
输出如下:
http://222:333@info.sporttery.cn/football/info/fb_match_hhad.php?m=102909#111
scheme:http
userInfo:222:333 没见过这东西-。-
host:info.sporttery.cn
path:/football/info/fb_match_hhad.php
queryParams:[m=102909]
fragment:111 路由
charset:GBK
更多,见文章标题下链接
https://blog.csdn.net/wxy1234556/article/details/79022402?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!