Skip to content

Commit 6c89387

Browse files
committed
New version
1 parent 11d0153 commit 6c89387

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jar {
2626

2727

2828
dependencies {
29-
compileOnly "com.github.kayjamlang:executor:0.1.3.17-fix1"
29+
compileOnly "com.github.kayjamlang:executor:0.1.3.17-fix5"
30+
implementation group: 'com.github.albfernandez', name: 'juniversalchardet', version: '2.4.0'
3031
testCompile group: 'junit', name: 'junit', version: '4.12'
3132
}

src/main/java/com/github/kayjamlang/request/RequestLibrary.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
import com.github.kayjamlang.executor.Context;
77
import com.github.kayjamlang.executor.libs.Library;
88
import com.github.kayjamlang.executor.libs.main.MapClass;
9+
import org.mozilla.universalchardet.UniversalDetector;
910

1011
import java.io.BufferedReader;
12+
import java.io.InputStream;
1113
import java.io.InputStreamReader;
1214
import java.net.HttpURLConnection;
1315
import java.net.URL;
1416
import java.net.URLEncoder;
1517
import java.util.ArrayList;
1618
import java.util.List;
1719
import java.util.Map;
20+
import java.util.Scanner;
1821

1922
public class RequestLibrary extends Library {
2023

@@ -25,10 +28,10 @@ public RequestLibrary() throws Exception {
2528

2629
List<String> query = new ArrayList<>();
2730
for(Map.Entry<Object, Object> entry: queryMap.entrySet()){
28-
query.add(entry.getKey()+"="+URLEncoder.encode((String) entry.getValue(), "UTF-8"));
31+
query.add(entry.getKey()+"="+URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
2932
}
3033

31-
return query.toString();
34+
return String.join("&", query);
3235
}, new Argument(new Type("map", ClassContainer.class, false),
3336
"query")));
3437

@@ -46,11 +49,13 @@ public RequestLibrary() throws Exception {
4649
connection.setRequestMethod((String) context.parentContext.variables.get("method"));
4750

4851
StringBuilder response = new StringBuilder();
49-
try(BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))){
50-
String inputLine;
52+
InputStream inputStream = connection.getResponseCode()==200?
53+
connection.getInputStream()
54+
:connection.getErrorStream();
55+
try(Scanner scanner = new Scanner(inputStream, "UTF-8")){
5156

52-
while ((inputLine = in.readLine()) != null)
53-
response.append(inputLine);
57+
while (scanner.hasNextLine())
58+
response.append(scanner.nextLine());
5459
}
5560

5661
return response.toString();

0 commit comments

Comments
 (0)