Im trying to send a postRequest using httpUrlConnection instead of using httpClient. The API that i am trying to access gave an implementation that used httpclinet that i can't use because it is now deprecated. I thought I had done the same thing using the urlConnection but I keep getting the error that no image has been sent (and that I have to make sure the POST request I'm sending matches the documentation).
This is the code provided by the API I am using.
public static String upload(String path) throws IllegalStateException, JSONException, IOException {
String urlString = "http://www.bitocr.com/api";
File file = new File(path);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(urlString);
// build request parameters
StringBody apiKey = new StringBody("apikey", ContentType.MULTIPART_FORM_DATA);
StringBody lang = new StringBody("en", ContentType.MULTIPART_FORM_DATA);
FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("apikey", apiKey);
builder.addPart("lang", lang);
builder.addPart("file", fileBody);
postRequest.setEntity(builder.build());
HttpResponse res = null;
// execute the request
res = httpClient.execute(postRequest);
JSONObject jsonObject = new JSONObject();
if (res != null) {
jsonObject = new JSONObject(IOUtils.toString(res.getEntity().getContent(), "UTF-8"));
String error = jsonObject.get("error").toString();
if (error.equals("0")) {
// success
System.out.println(jsonObject.getString("result"));
} else {
System.out.println("Error #" + jsonObject.get("error_code") + " " + jsonObject.get("error_message"));
}
}
return jsonObject.toString();
}
This is my implementation of Upload using urlConnection.
private String uploadURLCONN(String path){
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
String urlString = "http://www.bitocr.com/api";
File file = new File(path);
// build request parameters
StringBody apiKey = new StringBody("api_key", ContentType.MULTIPART_FORM_DATA);
StringBody lang = new StringBody("eng", ContentType.MULTIPART_FORM_DATA);
FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("apikey", apiKey);
builder.addPart("lang", lang);
builder.addPart("file", fileBody);
HttpEntity reqEntity = builder.build();
try {
URL url = new URL(urlString);
System.out.println("Not 1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("Not 2");
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
System.out.println("Not 3");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
conn.setRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
System.out.println("Not 4");
reqEntity.writeTo(conn.getOutputStream());
System.out.println("Not 5");
os.close();
System.out.println("Not 6");
conn.connect();
System.out.println("Not 7");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Not 8");
return readStream(conn.getInputStream());
}
}catch(Exception e){
System.out.println("multipart post error " + e + "(" + urlString + ")");
}
System.out.println("returning null");
return null;
}
I am using version 4.4 of httpClient and httpMime. And version 4.4.3 of httpCore. Any help on why my picture is not going through would be greatly appreciated.
I have ensured that the path that I'm using for the image does get the correct image and Upload() is within a correctly called AsyncTask. Thanks in advance.
This is what the documentation stated as what a raw request would look like.
POST /api HTTP/1.1
Content-Length:17778
Content-Type:multipart/form-data; boundary=-------------------------- -3339166599332
Host:www.bitocr.com
---------------------------3339166599332
Content-Disposition: form-data; name="file"; filename="Untitled.png"
Content-Type: image/png
{image data here}
---------------------------3339166599332
Content-Disposition: form-data; name="apikey"
a7412c8ac8c8d738
---------------------------3339166599332
Content-Disposition: form-data; name="lang"
eng
---------------------------3339166599332