Skip to content

Commit 5af3957

Browse files
committed
dev notes "+" vs "%20" in URI.
Conclusion is that these are not HTML calls; we should use %20.
1 parent 06a0148 commit 5af3957

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,15 @@ private Response fulfillGet(Response r) throws IOException {
322322

323323
private String encodeURI(String value) {
324324
try {
325+
// convert " " to "%20", not "+"
326+
// based on https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20
327+
// Answer # 46. and "URI Generic Syntax " https://tools.ietf.org/html/rfc3986
328+
// This will be consistent, then, with JavaScript encodeURIComponent().
325329
return URLEncoder.encode(value.replace(' ', '\0'), "UTF-8").replaceAll("%00", "%20");
326330
} catch (UnsupportedEncodingException e) {
327331
// impossible
332+
return null;
328333
}
329-
330-
return null;
331334
}
332335

333336
private Response fulfillPost(Response r) throws IOException {

sources/net.sf.j2s.java.core/src/test/Test_URL.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.net.URI;
1818
import java.net.URL;
1919
import java.net.URLConnection;
20+
import java.net.URLEncoder;
2021
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.Files;
2223
import java.nio.file.Path;
@@ -54,7 +55,10 @@ protected static String getResponseMimeType() {
5455
public static void main(String[] args) {
5556

5657
try {
57-
58+
String value = "this is a test+this";
59+
String s = URLEncoder.encode(value.replace(' ', '\0'), "UTF-8").replaceAll("%00", "%20");
60+
System.out.println(s);
61+
5862
testPost2();
5963

6064
// jar:file:C:/temp/car.trz!/Car in a loop with friction.trk

0 commit comments

Comments
 (0)