UploadSiteHostedPicture sample implemented in Java using Apache HttpClient library.
Detailed Description
The attached standalone sample project contains an implementation of the UploadSiteHostedPictures call. The project illusturates how to use Apache HttpClient to send a binary attachment, an image file to the target eBay EPS server:
2. open ./config.properties and fill out the following properties
# Your Application ID AppID= # Your Developer ID
DevID= # Your Developer Cert string
Cert= # eBay EPS server url EPS_ServerURL= # the image file that to be uploaded ImageFile= # the file that saves UploadSiteHostedPictureResponse document OutputFile= # an valid token for firing the UploadSiteHostedPicture request
token=
Note. A README.txt included in the sample project documents the usage of the project
Here are the main functions that showing the actual code of uploading picture via UploadSiteHostedPicutes API with HTTP post operation in the attached sample project:
/*
* CallerClient.execute() method
* 1. creates HttpClient and PostMethod objects * 2. specifies HTTP header with the parameters that required for sending the UploadSiteHostedPictures API
* request
* 3. makes Part array object
* 4. executes the Http Post operation
* 5. saves returned UploadSiteHostedPictures response XML document to a local file
*/
public void execute() {
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod(m_serverURL);
try {
if(PICTURE_UPLOAD.equals(m_type)) {
filePost.addRequestHeader("X-EBAY-API-CALL-NAME","UploadSiteHostedPictures"); // include both xml message and an attachment
Part[] parts = UploadAttachment.uploadFileAsAttachment(m_imageFileName,m_request);
if(parts != null){
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
}else{ // set UTF-8 encoding
StringRequestEntity req = new StringRequestEntity(m_request,"text/plain","UTF-8");
filePost.setRequestEntity(req);
}
}
filePost.addRequestHeader("SOAPAction","");
filePost.addRequestHeader("X-EBAY-API-SESSION-CERTIFICATE",m_appId+";"+m_devId+";"+m_cert);
filePost.addRequestHeader("X-EBAY-API-COMPATIBILITY-LEVEL","685");
filePost.addRequestHeader("X-EBAY-API-DEV-NAME",m_devId);
filePost.addRequestHeader("X-EBAY-API-APP-NAME",m_appId);
filePost.addRequestHeader("X-EBAY-API-CERT-NAME",m_cert);
filePost.addRequestHeader("X-EBAY-API-SITEID","0");
filePost.addRequestHeader("X-EBAY-API-DETAIL-LEVEL","0");
long startTime = System.currentTimeMillis(); // send messages int status = client.executeMethod(filePost);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
totalTime += elapsedTime;
System.out.println("It took " + elapsedTime + "ms to execute this API\n"); // redirect the XML response to a local file saveResponseToFile(filePost,m_outputFile);
public static void saveResponseToFile(PostMethod filePost, String outputFile){ //Get data as a String
try {
System.out.println( filePost.getRequestHeaders());
System.out.println(filePost.getResponseBodyAsString()); //OR as a byte array byte [] res = filePost.getResponseBody(); //write to file FileOutputStream fos= new FileOutputStream(outputFile);
fos.write(res);