code4craft/webmagic · error · IllegalArgumentException
illegal encoding
Error message
illegal encoding
What it means
HttpRequestBody.json() encodes the JSON string with the given charset; if the charset name is not supported on the JVM, UnsupportedEncodingException is caught and rethrown as IllegalArgumentException('illegal encoding ' + encoding).
Solutions
- Use a canonical constant: StandardCharsets.UTF_8.name()
- Normalize/trim the charset and validate with Charset.isSupported(encoding) before calling
- Fall back to UTF-8 when the encoding is unknown
Example fix
// before HttpRequestBody.json(json, "utf_8"); // after String encoding = "utf_8"; if (!Charset.isSupported(encoding)) encoding = StandardCharsets.UTF_8.name(); HttpRequestBody.json(json, encoding);
Defensive patterns
Strategy: validation
Validate before calling
if (!java.nio.charset.Charset.isSupported(encoding)) encoding = java.nio.charset.StandardCharsets.UTF_8.name();
Try / catch
try { HttpRequestBody.json(json, encoding); } catch (IllegalArgumentException e) { HttpRequestBody.json(json, "UTF-8"); } Prevention
- Use StandardCharsets constants, never hand-written charset strings
- Canonicalize charsets from headers/config before use
- Strip whitespace and check spelling ('UTF-8', not 'utf_8')
When it happens
Trigger: Calling HttpRequestBody.json(json, "utf8") or any misspelled charset ('utf-8 ' with trailing space, 'utf_8'); charset not present in the JVM's supported list.
Common situations: Hardcoding charset names from config or headers without canonicalization; typos like 'utf8' vs 'UTF-8' on strict JVM configurations; reading encoding from an HTTP Content-Type header.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- threadNum should be more than one!
- emptySleepTime should be more than zero!
- task or site can not be null
- Illegal HTTP Method
- language and script must not be null!
AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08).
Data as JSON: /api/errors/91cdca7e7468528c.
Report an issue: GitHub.
Appendix: source
Thrown at webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java:71
}
public void setBody(byte[] body) {
this.body = body;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public static HttpRequestBody json(String json, String encoding) {
try {
return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
}
}
public static HttpRequestBody xml(String xml, String encoding) {
try {
return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
}
}
public static HttpRequestBody custom(byte[] body, String contentType, String encoding) {
return new HttpRequestBody(body, contentType, encoding);
}
public static HttpRequestBody form(Map<String,Object> params, String encoding){
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {View on GitHub (pinned to 67816a19d6)