I have a CURL as follows. When I call this from the command prompt, after process it returns JSON value. If I access this CURL from PHP also it gives the proper result. Now I want to access this in Java to integrate it to a java project.
curl xx.xx.xx.xx:5000/models/images/one.json -XPOST -F job_id=20yy0811-wq50r5-b629 -F image_url=http://www.mysite/public/testimage.jpg
I tried to implement it some examples I got from internet, shows HTTP errors like 400, 405 etc.
String stringUrl = "http://xx.xx.xx.xx:5000/models/images/one.json";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
uc.setRequestProperty("format","json");
uc.setRequestProperty("job_id", "20yy0811-wq50r5-b629");
uc.setRequestProperty("image_url", "http://www.mysite/public/testimage.jpg");
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
It gives the result:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL:
Tried another code:
String url = "http://xx.xx.xx.xx:5000/models/images/one.json";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("job_id", "20yy0811-wq50r5-b629");
conn.setRequestProperty("image_url", "http://www.mysite/public/testimage.jpg");
String data = "{\"format\":\"json\"}";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
new InputStreamReader(conn.getInputStream());
It gives the result:
java.io.IOException: Server returned HTTP response code: 400 for URL:
My requirement is:
URL: xx.xx.xx.xx:5000/models/images/one.json
Parameters: job_id, image_url
Return value : json
How can I convert this CURL to java code? If someone can change this CURL to Java code it would be great.
SOLUTION:
- Download Apache HttpComponents
Follow the steps in http://www.journaldev.com/7146/apache-httpclient-example-to-send-get-post-http-requests
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://xx.xx.xx.xx:5000/models/images/one.json"); //httpPost.addHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("job_id", "20yy0811-wq50r5-b629")); urlParameters.add(new BasicNameValuePair("image_url", "http://www.mysite/public/testimage.jpg")); HttpEntity postParams = new UrlEncodedFormEntity(urlParameters); httpPost.setEntity(postParams); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); System.out.println("POST Response Status:: " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { response.append(inputLine); } reader.close(); // print result System.out.println(response.toString()); httpClient.close();