URLBuilder简单快速构建URL链接

Posted 阿奇XS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URLBuilder简单快速构建URL链接相关的知识,希望对你有一定的参考价值。

URLBuilder快速构建一个URL结构,使用了Builder建造者模式,大大增加了代码的可读性,并且可以支持参数URL编码。


package com.kaishustory.quick.commons.text;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class URLBuilder 
	private static boolean[] eucIgnore = new boolean[256];

	protected StringBuilder builder = new StringBuilder();
	protected boolean firstParam = true;
	protected boolean hasPath = false;

	private static String percentify(String s) 
		StringBuilder sb = new StringBuilder();

		ByteBuffer bb = Charset.forName("utf-8").encode(s);
		int size = bb.limit();
		for (int i = 0; i < size; i++) 
			sb.append(String.format("%%%02x", new Object[]  Integer.valueOf(bb.get() & 0xFF) ));
		

		return sb.toString();
	

	public static String encodeURIComponent(String s) 
		if (s == null) 
			return null;
		
		if ("".equals(s)) 
			return "";
		
		StringBuilder sb = new StringBuilder();

		int _i = 0;
		int c = Character.codePointAt(s, _i);
		boolean ignore = (c < 256) && (eucIgnore[c]!=false);

		for (int i = 0; i < s.length(); i++) 
			c = Character.codePointAt(s, i);
			if (ignore != ((c < 256) && (eucIgnore[c] != false))) 
				if (ignore)
					sb.append(s.substring(_i, i));
				else
					sb.append(percentify(s.substring(_i, i)));
				ignore = !ignore;
				_i = i;
			
		

		if (ignore)
			sb.append(s.substring(_i, s.length()));
		else 
			sb.append(percentify(s.substring(_i, s.length())));
		

		return sb.toString();
	

	protected void appendParamPrefix() 
		if (this.firstParam) 
			this.firstParam = false;
			if (this.hasPath)
				this.builder.append('?');
		 else 
			this.builder.append('&');
		
	

	public URLBuilder appendPath(String path) 
		if (path == null) 
			return this;
		
		if ((this.hasPath) || (!this.firstParam)) 
			throw new IllegalStateException("Missed the trick to set path.");
		
		this.hasPath = true;

		this.builder.append(path);

		return this;
	

	public URLBuilder appendParam(String key, String value) 
		if ((key != null) && (value != null)) 
			appendParamPrefix();
			this.builder.append(key).append('=');
			this.builder.append(value);
		

		return this;
	

	public URLBuilder appendParamEncode(String key, String value) 
		if ((key != null) && (value != null)) 
			appendParamPrefix();
			this.builder.append(key).append('=');
			this.builder.append(encodeURIComponent(value));
		

		return this;
	

	public URLBuilder appendParamEncode(String key, String value, String charset) 
		appendParamEncode(key, value);

		return this;
	

	public URLBuilder appendLabel(String label) 
		this.builder.append('#').append(label);

		return this;
	

	public URLBuilder append(String str) 
		this.builder.append(str);

		return this;
	

	public String toString() 
		return this.builder.toString();
	

	static 
		String ignore = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-_.!~*'()";
		for (int i = 0; i < ignore.length(); i++)
			eucIgnore[Character.codePointAt(ignore, i)] = true;
	
	
	public static void main(String[] args)
		URLBuilder urlBuilder = new URLBuilder();
		urlBuilder.
			appendPath("https://weixin.kaishustory.com").
			appendParam("key1", "value1").
			appendParam("key2", "value2").appendParamEncode("ekey3", "$#你好").appendLabel("label");
		System.err.println(urlBuilder.toString());

	

//https://weixin.kaishustory.com?key1=value1&key2=value2&ekey3=%7b%24%23%e4%bd%a0%e5%a5%bd%7d#label

以上是关于URLBuilder简单快速构建URL链接的主要内容,如果未能解决你的问题,请参考以下文章

java UrlBuilder.java

csharp UrlBuilder

Dart/Flutter:快速检查 404 链接列表

AWS Cloudformation 快速创建链接未读取 URL 中的参数

制作scrapy蜘蛛跟随给定起始URL的链接

Xcode 9.3 使用 URL 方案(深度链接)调用时构建崩溃 [重复]