1

I am using multipartEntity to upload videos to the server. Here is my code:

public boolean uploadVideoToStickyworld(String fileName, String title, String description, String iEndpointURI)
    {
        boolean retBool = false;

        try {

        HttpPost httpPost = new HttpPost(iEndpointURI);
        httpPost.setHeader("User-Agent", userAgent);
        httpPost.setHeader("Authorization", "Basic " + usnHeader);

        try {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost httppost = new HttpPost(iEndpointURI);
        File file = new File(fileName);

        MultipartEntity mpEntity = new MultipartEntity();
        FileBody fileBody = new FileBody(file);
        mpEntity.addPart("title",new StringBody(title));
        mpEntity.addPart("description", new StringBody(description));
        mpEntity.addPart("video", fileBody);

        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine()+ "header "+ httppost.getURI());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        System.out.println(response.getStatusLine());

        if (resEntity != null) {
          resEntity.consumeContent();
          System.out.println(EntityUtils.toString(resEntity));
          retBool = true;
        }

        }catch (Exception e) {
        }

    } catch(Exception e){

    }
    return retBool;
}

the code is from How to upload a file using Java HttpClient library working with PHP

But the response from server is bad request: 400

Because the server uses multipart encoding? who can tell me is there something wrong with this code? Thank you so much.

Community
  • 1
  • 1
ruthot
  • 31
  • 2
  • 400 status code is sent from the server when your request is incomplete or bad syntax. Without knowing what the API requires we can't tell you what is wrong with your request. – Robby Pond Feb 26 '14 at 17:24

0 Answers0