Attachment anlegen: java.io.IOException: Server returned HTTP response code: 500

118 views
Skip to first unread message

Thomas Müller

unread,
Jun 26, 2014, 3:06:17 PM6/26/14
to immobilienscou...@googlegroups.com
Hallo zusammen,

leider scheitere ich mit einem Java Programm ein Bild/PDF Attachment anzulegen. Der Request sieht im Sniffer so aus:

POST /restapi/api/offer/v1.0/user/me/realestate/75328506/attachment/ HTTP/1.1
Content-Type: multipart/form-data; boundary="-----1403809023583"
Accept-Encoding: gzip,deflate
Authorization: OAuth oauth_token="xxx", oauth_consumer_key="XXXKey", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1403809023", oauth_nonce="-646520117001656342", oauth_signature="xxx"
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.6.0_45
Host: rest.immobilienscout24.de
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 596119

*****SNIP BINARY_DATA SNIP****
-----1403809023583
Content-Disposition: form-data; name="attachment"; filename="1403809023583.jpg"
Content-Type: image/jpeg; name=1403809023583.jpg
Content-Transfer-Encoding: binary



-----1403809023583

Content-Disposition: form-data; name="metadata"; filename="body.xml"
Content-Transfer-Encoding: binary
Content-Type: text/xml; charset=UTF-8; name=body.xml

<?xml version="1.0"?>
<common:attachment xsi:type="common:Picture" xmlns:common="http://rest.immobilienscout24.de/schema/common/1.0" xmlns:ns3="http://rest.immobilienscout24.de/schema/platform/gis/1.0"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <title>sehr schoenes Bild 1</title>
  <floorplan>false</floorplan>
  <titlePicture>false</titlePicture>
</common:attachment>
-----1403809023583


Und das ist die Reponse:
HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Date: Thu, 26 Jun 2014 18:57:03 GMT
Content-Type: application/xml;charset=UTF-8
Content-Length: 0
X
-Cnection: close

Was dann in dieser Exception endet:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://rest.immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/75328506/attachment/
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1514)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1508)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1162)
    at c.b.is24.restapi.sandbox.attachment.Oauth2LegSignpostPOST.main(Oauth2LegSignpostPOST.java:185)
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://rest.immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/75328506/attachment/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
    at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:445)
    at c.b.is24.restapi.sandbox.attachment.Oauth2LegSignpostPOST.main(Oauth2LegSignpostPOST.java:169)


Kann mir jemand helfen? Vielen Dank schon mal!!

Viele Grüße
Thomas

Thomas Müller

unread,
Jun 27, 2014, 7:19:19 AM6/27/14
to immobilienscou...@googlegroups.com
Vielleicht kann mir auch jemand mit einem funktionierendem Java Code Beispiel zum Hochladen von Attachments weiterhelfen?
Vielen Dank!

Thomas Müller

unread,
Jun 27, 2014, 3:11:35 PM6/27/14
to immobilienscou...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages