saini
saini

Reputation: 233

Android uploading file as ByteArray to WCF REST services

I need to send a byte arrray file using WCF rest services. I have to send the data using HttpPost method in android. The code which i am using give the status as HTTP/1.1 400 Bad Request error.

  private final static String URI = "http://192.168.1.15/QueryService/Import/Test";
         final HttpPost request = new HttpPost(URI);
            final HttpClient httpClient = new DefaultHttpClient();
            final ByteArrayEntity entity = new ByteArrayEntity(fileToBytes(pathToOurFile));
            entity.setContentType("application/octet-stream");
            entity.setChunked(true);
            request.setEntity(entity);
            final HttpResponse hr = httpClient.execute(request);
           final StatusLine status = hr.getStatusLine();
        httpClient.getConnectionManager().shutdown();

Upvotes: 1

Views: 1291

Answers (2)

Anvesh
Anvesh

Reputation: 319

I was also facing same problem with WCF service. 400 Bad request means request parameter value which you are passing to method doesn't match with method's parameter. I have used Base64 string encoding to pass file as method parameter. May it'll help you.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104188

It is difficult to tell what is wrong with your request. The standard way of resolving this kind of errors is:

  • Create a WCF client for your service. Verify that it works as expected.
  • Use Fiddler or another suitable tool to intercept the HTTP request your client is generating. Both the headers and the body are important.
  • Modify your Android request to generate the exact same request as the WCF client.

Upvotes: 1

Related Questions