1

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();
saini
  • 233
  • 3
  • 10

2 Answers2

1

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.
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • i use Fiddler but it does't show the http request made by my application. I have also change the emulator proxy to fiddler ip address. when i browse any website through emulator it shows in the Fiddler but it does't show my application request in fiddler. Also Same code work for StringEntity but does't show any process in Fiddler. – saini Dec 23 '11 at 10:49
  • This sounds like an emulator issue. You can use an in-between client to easily intercept the request. I have successfully used http://code.google.com/p/tcpmon/ for this in the past. – kgiannakakis Dec 23 '11 at 11:06
  • i can send the request to services but it seems that the request which i am sending is not in proper format which i have to receive at web service. Also i try to send the data in stream format through multipartentity but does't get the any responce. – saini Dec 30 '11 at 08:22
0

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.

Anvesh
  • 309
  • 1
  • 8
  • 24