gpt4 book ai didi

java - 使用 java 的 cURL 命令

转载 作者:行者123 更新时间:2023-12-01 05:00:24 24 4
gpt4 key购买 nike

curl -F file=@/path/to/index.html -u lslkdfmkls@gmail.com -F 'data={"title":"API V1  App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps 

我正在尝试使用 HttpClient 库使用 java 程序来实现相同的目的。

DefaultHttpClient client = new DefaultHttpClient(); 
HttpHost targetHost = new HttpHost("build.phonegap.com", 443, "https");
client.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort(),AuthScope.ANY_REALM),
new UsernamePasswordCredentials("abc@gmail.com", "abc123"));

String authToken = "?auth_token=abcdefgh";

HttpPost httpPost = new HttpPost("https://build.phonegap.com/api/v1/apps" + authToken );

String jsonString = "{\"title\":\"API V1 App\",\"create_method\":\"file\"}";
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart(new FormBodyPart("data", new StringBody(jsonString)));
multipartEntity.addPart("file", new FileBody(new File("C:/Users/Desktop/app.zip")));

/*StringEntity entity = new StringEntity(jsonString, "UTF-8"); */
httpPost.setEntity(multipartEntity);

System.out.println("executing request " + httpPost.getRequestLine());
HttpResponse httpResponse = client.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
if(entity != null ){
System.out.println(EntityUtils.toString(entity));
}

在上面的代码中,我只能设置 StringEntity 或 FileEntity,但不能同时设置两者,我认为这是获得 curl 命令功能所需的。

尝试使用 StringEntity 和 FileEntity 后,我尝试使用 MultipartEntity 但没有运气..您能否向我提供更多详细信息,如果可能的话,提供一个示例..

提前致谢。

最佳答案

必须按如下方式实例化 MultipartEntity:

MultipartEntity multipartEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE
) ;

这对我有用。

默认情况下 MultipartEntity实例化为 HttpMultipartMode.STRICT模式在 javadoc 中记录为“RFC 822、RFC 2045、RFC 2046 兼容”。

有人可以简要介绍一下这里提到的 RFC,以便清楚理解..

非常感谢

关于java - 使用 java 的 cURL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com