1
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 を試しましたが、うまくいきませんでした。詳細と、可能であれば例を教えてください。

前もって感謝します。

4

1 に答える 1

0

次のように MultipartEntity をインスタンス化する必要があります。

MultipartEntity multipartEntity = new MultipartEntity(
                                         HttpMultipartMode.BROWSER_COMPATIBLE 
                                                       ) ;

これは私にとってはうまくいきました。

デフォルトでは、「RFC 822、RFC 2045、RFC 2046 準拠」として javadocs に記載されているモードでMultipartEntityインスタンス化されます。HttpMultipartMode.STRICT

明確な理解のために、誰かがここで言及されている RFC を簡単に説明できますか..

どうもありがとう

于 2012-11-21T15:58:24.033 に答える