rename FormMap to ArgMap then support url arg

This commit is contained in:
Timi
2025-10-13 10:52:48 +08:00
parent 016e0b0962
commit 016c0b7e2f

View File

@ -1,5 +1,6 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.utils.Encoder;
import org.apache.hc.client5.http.fluent.Form; import org.apache.hc.client5.http.fluent.Form;
import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.NameValuePair;
@ -13,13 +14,27 @@ import java.util.Map;
* @author 夜雨 * @author 夜雨
* @since 2025-06-26 15:41 * @since 2025-06-26 15:41
*/ */
public class FormMap<K, V> extends HashMap<K, V> { public class ArgMap<K, V> extends HashMap<K, V> {
public List<NameValuePair> build() { public List<NameValuePair> toNameValuePair() {
Form form = Form.form(); Form form = Form.form();
for (Map.Entry<K, V> item : entrySet()) { for (Map.Entry<K, V> item : entrySet()) {
form.add(item.getKey().toString(), item.getValue().toString()); form.add(item.getKey().toString(), item.getValue().toString());
} }
return form.build(); return form.build();
} }
public String toURL() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<K, V> item : entrySet()) {
sb.append(item.getKey().toString()).append('=').append(Encoder.urlArg(item.getValue().toString()));
sb.append('&');
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public String toURL(String url) {
return url + "?" + toURL();
}
} }