Skip to content

Commit 62e66fb

Browse files
committed
JSHttpClient successful test #1 and #2
1 parent db66db4 commit 62e66fb

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public class HttpClientFactory {
1616

17-
private static String defaultClassName = "javajs.util.JSHttpClient";
17+
private static String defaultClassName = "javajs.http.JSHttpClient";
1818

1919
public static void setDefaultClassName(String className) {
2020
if (className != null)

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ public HttpResponse executeAsync(Consumer<? super HttpResponse> succeed,
231231
public void run() {
232232
try {
233233
if (hasFormBody)
234-
fulfillPost();
234+
fulfillPost(r);
235235
else
236-
fulfillGet();
236+
fulfillGet(r);
237237
} catch (Exception e) {
238238
r.handleError(e);
239239
}
@@ -248,7 +248,7 @@ public void run() {
248248
}
249249

250250
@SuppressWarnings("resource")
251-
public Response fulfillGet() throws Exception {
251+
public Response fulfillGet(Response r) throws Exception {
252252
URI uri = getUri();
253253
String data = "";
254254
for (Entry<String, String> e : htGetParams.entrySet()) {
@@ -262,12 +262,11 @@ public Response fulfillGet() throws Exception {
262262
uri = new URI(uri.toString() + "?" + data);
263263
}
264264
getConnection(uri);
265-
return new Response().getResponse();
265+
return r.getResponse();
266266
}
267267

268-
public Response fulfillPost() throws IOException {
268+
public Response fulfillPost(Response r) throws IOException {
269269
getConnection(uri);
270-
Response r = new Response();
271270
for (int i = 0; i < listPostFiles.size(); i++) {
272271
Object[] name_data = listPostFiles.get(i);
273272
String name = (String) name_data[0];
@@ -446,7 +445,7 @@ public Map<String, String> getHeaders() {
446445
public String getText() throws IOException {
447446
return new String(getContent().readAllBytes());
448447
}
449-
448+
450449
/**
451450
* In SwingJS, this is always a ByteArrayInputStream.
452451
*/
@@ -476,6 +475,11 @@ public void close() {
476475
}
477476
}
478477

478+
@Override
479+
public String toString() {
480+
return "JSHttpClient " + method + " state=" + state + " uri=" + uri;
481+
}
482+
479483
}
480484

481485
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package test;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
5+
import java.net.URI;
6+
import java.util.function.Consumer;
7+
8+
import javajs.http.HttpClient;
9+
import javajs.http.HttpClient.HttpRequest;
10+
import javajs.http.HttpClient.HttpResponse;
11+
import javajs.http.HttpClientFactory;
12+
13+
/** A JavaScript-only class */
14+
15+
public class Test_HTTP extends Test_ {
16+
17+
@SuppressWarnings("unused")
18+
public static void main(String[] args) {
19+
20+
if (/** @j2sNative false && */ true)
21+
return;
22+
try {
23+
24+
HttpClient client = HttpClientFactory.getClient(null);
25+
26+
HttpRequest req = client.get(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services"));
27+
HttpResponse resp = req.execute();
28+
29+
System.out.println(resp);
30+
System.out.println(resp.getText());
31+
32+
req = client.put(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services/example"));
33+
34+
String json = "{\"key1\":\"val1\", \"key2\":\"val2\"}";
35+
req.addFile("input-file",new ByteArrayInputStream(json.getBytes()));
36+
req.addParameter("content", "len:11 long");
37+
38+
resp = req.execute();
39+
40+
System.out.println(resp);
41+
System.out.println(resp.getText());
42+
43+
44+
// req = client.put(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services/example"));
45+
//
46+
// req.addFile("input-file",new ByteArrayInputStream(json.getBytes()));
47+
// req.addParameter("content", "len:11 long");
48+
//
49+
// resp = req.executeAsync(new Consumer<HttpResponse>() {
50+
//
51+
// @Override
52+
// public void accept(HttpResponse t) {
53+
// try {
54+
// System.out.println(t.getText());
55+
// } catch (IOException e) {
56+
// // TODO Auto-generated catch block
57+
// e.printStackTrace();
58+
// }
59+
// }
60+
//
61+
// }, null, null);
62+
63+
64+
} catch (Exception e3) {
65+
e3.printStackTrace();
66+
}
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)