So, nun habe ich es hinbekommen, mit dem Apache HTTPClient 4.3.4 hat es funktioniert. Ich tippe drauf, dass der Fehler im Streamhandling lag.
Hier die Maven dependencies:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.4</version>
</dependency>
Und hier ein komplettes Beispiel für einen Imageupload mit 2-leg-OAuth:
import java.io.File;
import java.io.IOException;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Oauth2LegHttpClientPOST {
private static String boundary;
private static String imageFileName = "Hydrangeas.jpg";
private static String imageFileNameForRequest = +System.currentTimeMillis() + ".jpg";
private static Class<Oauth2LegHttpClientPOST> thisClass = Oauth2LegHttpClientPOST.class;
public static void main(String[] args) throws Exception {
String extObjectId = "externalObjectId";
boundary = "-----" + System.currentTimeMillis();
OAuthConsumer consumer = new CommonsHttpOAuthConsumer("CONSUMER_KEY", "CONSUMER_SECRET");
consumer.setTokenWithSecret("TOKEN", "TOKEN_SECRET");
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(
"http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/" + extObjectId
+ "/attachment");
httppost.setHeader("Content-Type", "multipart/form-data; boundary=\"" + boundary + "\"");
httppost.setHeader("Accept-Encoding", "gzip,deflate");
consumer.sign(httppost);
HttpEntity entity = MultipartEntityBuilder.create().setBoundary(boundary)
.addBinaryBody("attachment", getImage(), ContentType.create("image/jpeg"), imageFileNameForRequest)
.addBinaryBody("metadata", getMetaDataContent().getBytes(), ContentType.APPLICATION_XML, "body.xml").build();
httppost.setEntity(entity);
System.out.println("executing request " + httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
}
finally {
response.close();
}
}
finally {
httpclient.close();
}
}
private static String getMetaDataContent() {
try {
return FileUtils.readFileToString(new File("src/main/java/"
+ thisClass.getPackage().getName().replaceAll("\\.", "/") + "/attachment1.xml"));
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static byte[] getImage() {
try {
return FileUtils.readFileToByteArray(new File("src/main/java/"
+ thisClass.getPackage().getName().replaceAll("\\.", "/") + "/" + imageFileName));
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Viele Grüße
Thomas