Reputation: 787
I am trying to create a java application to do the following which works in CURL on Windows.
curl -x XXX.XXX.XXX.XXX:8080 -X POST --data-binary "@C:\Users\XXXXX\Documents\batch2.txt" http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX
I have been able to replicate the call taking the contents of the file and posting it direct to the URL in java using HttpURLConnection however this is not viable and would prefer to use the file method as it can be rather large.
The code I currently have is:
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.mime.HttpMultipartMode;
public class PostFile {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX");
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
.setProxy(new HttpHost("XXX.XXX.XXX.XXX", 8080))
.build();
httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type","data/binary");
File file = new File("C:/Users/XXXXXX/Documents/batch2.txt");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("batch", file);
final HttpEntity entity = builder.build();
httppost.setEntity(entity);
System.out.println("executing request " + httppost.getRequestLine() + httppost.getConfig());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {;
System.out.println(EntityUtils.toString(resEntity));
}
httpclient.close();
}
}
The issue I am having is that it is not providing the same response and seems as if it is failing to POST the file.
Upvotes: 0
Views: 10539
Reputation: 787
Thank you Andreas for hinting towards the content type,
I have changed this and changed from a MultiPart Entity to a FileEntity and now works fine.
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.mime.HttpMultipartMode;
public class PostFile {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX");
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
.setProxy(new HttpHost("XXX.XXX.XXX.XXX", 8080))
.build();
httppost.setConfig(requestConfig);
httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
File file = new File("batch2.txt");
FileEntity entity = new FileEntity(file);
httppost.setEntity(entity);
System.out.println("executing request " + httppost.getRequestLine() + httppost.getConfig());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {;
System.out.println(EntityUtils.toString(resEntity));
}
httpclient.close();
}
}
Upvotes: 1