leveraging http client

This commit is contained in:
rusefi 2020-05-22 14:03:10 -04:00
parent c681ee659b
commit a5b2939386
8 changed files with 38 additions and 50 deletions

View File

@ -0,0 +1,14 @@
<component name="libraryTable">
<library name="httpclient">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/httpclient.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/httpmime.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/httpcore.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$PROJECT_DIR$/lib/httpclient-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/httpmime-javadoc.jar!/" />
</JAVADOC>
<SOURCES />
</library>
</component>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,64 +1,37 @@
package com.rusefi.tools.online;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
public class Online {
static final String charset = "UTF-8";
static final String url = "https://rusefi.com/online/upload.php";
static String CRLF = "\r\n"; // Line separator required by multipart/form-data.
private static final String url = "https://rusefi.com/online/upload.php";
public static void upload(File xmlFile, String authTokenValue) throws IOException {
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
FileBody uploadFilePart = new FileBody(xmlFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
reqEntity.addPart("auth_token", new StringBody(authTokenValue));
try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
sendParameter(boundary, writer, "auth_token", authTokenValue);
httpPost.setEntity(reqEntity);
// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + xmlFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(xmlFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
HttpResponse response = httpclient.execute(httpPost);
System.out.println(response);
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
}
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
int responseCode = httpURLConnection.getResponseCode();
System.out.println(responseCode); // Should be 200
BufferedReader br;
if (responseCode >= 200 && responseCode <= 299) {
br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
} else {
br = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
}
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}
private static void sendParameter(String boundary, PrintWriter writer, String parameterName, String value) {
// Send normal param.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + parameterName +"\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append(value).append(CRLF).flush();
}
}

View File

@ -18,5 +18,6 @@
<orderEntry type="module" module-name="romraider" />
<orderEntry type="library" name="jlatexmath" level="project" />
<orderEntry type="module" module-name="configuration_definition" />
<orderEntry type="library" name="httpclient" level="project" />
</component>
</module>