Skip to content

Commit aa42c8e

Browse files
committed
HttpClient v 2
- JSHttpClient renamed SimpleHttpClient - Java or JavaScript - no org.apache.http dependence - adds addFormField with contentType and file name - adds removeFormField - combines htParams and formData into one list
1 parent 2a8774e commit aa42c8e

File tree

5 files changed

+238
-157
lines changed

5 files changed

+238
-157
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public interface HttpRequest {
3232

3333
public HttpRequest addFile(String name, InputStream stream);
3434

35+
public HttpRequest addFormField(String name, Object data, String contentType, String fileName);
36+
37+
boolean removeFormField(String name);
38+
3539
/**
3640
* Send the request to the server and return the response.
3741
*/

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.http.JSHttpClient";
17+
private static String defaultClassName = "javajs.http.SimpleHttpClient";
1818

1919
public static void setDefaultClassName(String className) {
2020
if (className != null)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package javajs.http;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.net.HttpURLConnection;
6+
import java.util.List;
7+
8+
import javajs.http.SimpleHttpClient.Request.FormData;
9+
10+
/**
11+
* A simple Java byte[] or String poster.
12+
*
13+
* A java client that implements multipart form submit over https implemented
14+
* with only jdk classes and no external dependencies.
15+
*
16+
* Not used in SwingJ. HTML5 does not allow setting content-type to
17+
* multipart/form-data. Instead, for JavaScript, we simply load up an array of
18+
* information and turn that into a FormData object in AjaxURLConnection.
19+
*
20+
* adapted from https://github.com/atulsm/https-multipart-purejava
21+
*
22+
* @author Bob Hanson
23+
*
24+
*/
25+
public class JavaHttpPoster {
26+
27+
public static void post(HttpURLConnection conn, List<FormData> formData) throws IOException {
28+
String boundary = "---" + System.nanoTime() + "---";
29+
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
30+
OutputStream os = conn.getOutputStream();
31+
for (int i = 0; i < formData.size(); i++) {
32+
FormData data = formData.get(i);
33+
String name = data.getName();
34+
Object value = data.getData();
35+
String contentType = data.getContentType();
36+
String fileName = data.getFileName();
37+
append(os, "--" + boundary + "\r\n");
38+
append(os, "Content-Disposition: form-data; name=\"" + name
39+
+ (fileName == null ? "" : "\"; filename=\"" + fileName) + "\"");
40+
append(os, "\r\nContent-Type: ");
41+
append(os, contentType == null ? "application/octet-stream" : contentType);
42+
append(os, "\r\n\r\n");
43+
append(os, value);
44+
append(os, "\r\n");
45+
}
46+
append(os, "\r\n--" + boundary + "--\r\n");
47+
os.flush();
48+
49+
}
50+
51+
private static void append(OutputStream outputStream, Object val) throws IOException {
52+
if (val instanceof byte[]) {
53+
outputStream.write((byte[]) val);
54+
} else {
55+
outputStream.write(val.toString().getBytes());
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)