简体   繁体   English

Java等同于curl命令

[英]Java equivalent to curl command

I am currently attempting to figure out the Java equivalent to this curl command: 我目前正在尝试找出等效于此curl命令的Java:

curl -X POST -u username:password -H "X-Atlassian-Token: no-check" http://example.com/rest/api/1.0/projects/STASH/avatar.png -F avatar=@avatar.png

Any help would be appreciated. 任何帮助,将不胜感激。

So far, I've had success using the Apache HTTP library. 到目前为止,我已经成功使用了Apache HTTP库。 Below is an example of a POST request that I have used successfully. 以下是我已成功使用的POST请求的示例。 However, this example is the equivalent of this curl command: 但是,此示例等效于以下curl命令:

curl -X POST -u username:password -H "Content-type: application/json" --data '{\"name\":\"projectName\", \"key\":\"KEY\", \"description\":\"good?\"}' "http://localhost:7990/rest/api/1.0/projects"

and the java equivalent: 和Java等效项:

// initialize connection
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:7990/rest/api/1.0/projects")
try
{
    // create a request using the input
    StringEntity request = new StringEntity("{\"name\":\"projectName\", \"key\":\"KEY\", \"description\":\"good?\"}",ContentType.APPLICATION_JSON);
    post.setEntity(request);
    // add credentials to the header in order to get authorization
    String credentials = username + ":" + password
    byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes("UTF-8"));
    String header = "Basic " + new String(encodedCredentials);
    post.addHeader("Authorization",header);
    // execute the request using the POST method
    client.execute(post);
}
catch(Exception e)
{
    // nada
}
finally
{
    // close the connection
    post.releaseConnection();
}

This is what I've come up with for mimicking the curl command I first mentioned: 这是我模仿我第一次提到的curl命令时想到的:

// initialize connection
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(HOST_URL + uri);
try
{
    // create a request using the input
    File avatar = new File("avatar.png")
    FileBody uploadFilePart = new FileBody(avatar);
    MultipartEntity request = new MultipartEntity();
    request.addPart("upload-file", uploadFilePart);
    post.setEntity(request);
    // add credentials to the header in order to get authorization
    byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes("UTF-8"));
    String header = "Basic " + new String(encodedCredentials);
    post.addHeader("Authorization",header);
    // add the other header peice
    post.addHeader("X-Atlassian-Token","no-check");
    // execute the request
    client.execute(post);
}
catch(Exception e)
{
    // nada
}
finally
{
    // close the connection
    post.releaseConnection();
}

I think its just the file uploading part that's tripping me up. 我认为这只是文件上传的一部分,这让我很烦。 I know the original curl request works, I've run it in git bash successfully. 我知道原始的curl请求有效,我已经在git bash中成功运行了它。

While searching for the proper way to upload files I've come across examples that use different versions of multi-part data, such as the MultipartEntityBuilder or MultipartRequestEntity. 在寻找正确的上传文件方式时,我遇到了使用不同版本的多部分数据的示例,例如MultipartEntityBuilder或MultipartRequestEntity。 But so far I haven't had success with any of them (That's not to say they were wrong, I just don't know what I'm doing). 但是到目前为止,我还没有取得成功(这并不是说他们错了,我只是不知道自己在做什么)。

You can make use of java.net.URL and/or java.net.URLConnection. 您可以使用java.net.URL和/或java.net.URLConnection.

URL url = new URL("http://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

Also see the Oracle's simple tutorial on the subject. 另请参阅有关该主题的Oracle简单教程。 It's however a bit verbose. 但是,它有点冗长。 To end up with less verbose code, you may want to consider Apache HttpClient instead. 为了减少冗长的代码结尾,您可能需要考虑使用Apache HttpClient。

See 看到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM