将CURL转换为Java代码

时间:2015-08-12 11:43:57

标签: java curl

我有一个CURL如下。当我从命令提示符调用它时,在进程之后它返回JSON值。如果我从PHP访问此CURL,它也会给出正确的结果。现在我想用Java访问它以将它集成到java项目中。

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

我试图实现它从互联网上获得的一些例子,显示了400,405等HTTP错误。

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()); 

它给出了结果:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL:

尝试了另一个代码:

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()); 

它给出了结果:

java.io.IOException: Server returned HTTP response code: 400 for URL:

我的要求是:

网址:xx.xx.xx.xx:5000 / models / images / one.json

参数:job_id,image_url

返回值:json

如何将此CURL转换为java代码?如果有人可以将这个CURL更改为Java代码,那就太棒了。

解决方案:

  • 下载Apache HttpComponents
  • 按照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();
    

1 个答案:

答案 0 :(得分:1)

setRequestProperty()设置标题。要设置表单参数,请尝试以下操作:

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");

//build it this way
Uri.Builder builder = new Uri.Builder()
        .appendQueryParameter("firstParam", paramValue1)
        .appendQueryParameter("secondParam", paramValue2)
        .appendQueryParameter("thirdParam", paramValue3);
String query = builder.build().getEncodedQuery();

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();

您需要导入ApacheHttpClient lib以使用Uri.Builder类。

来源:https://stackoverflow.com/a/29053050/856007