0

I am trying to call a WS method from an Android application with POST method. What I have done:

String urlServer = GlobalSession.IP + "insert_reportByte";
            Log.d("[Report]", "url address: " + urlServer);
            URL url = new URL(urlServer);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data");

            DataOutputStream outputStream = new DataOutputStream(
                    connection.getOutputStream());

            outputStream.write(outputByteArray, 0, outputByteArray.length);
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.d("ServerCode", "" + serverResponseCode);
            Log.d("serverResponseMessage", "" + serverResponseMessage);
            outputStream.flush();
            outputStream.close();

        } catch (Exception ex) {
            // ex.printStackTrace();
            Log.e("[Report] --- /!\\ Error: ", ex.getMessage());
        }
        return result;

So I am supposed to send a byte array to the service. But I have a 400 error response. My question is: how to get the details of such an issue? Because I cannot find anything in the logs of the server and it's hard to debug if I do not have the details...

The WS is defined (in ASP.NET) that way:

[OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "insert_reportByte",
        BodyStyle = WebMessageBodyStyle.Bare)]
    void insert_reportByte(byte[] image);

And the called method is the following

public void insert_reportByte(byte[] image)
    {
        MyEntities entities = new MyEntities();
        String base64stringimage = System.Convert.ToBase64String(image,0,image.Length);

        entities.insert_report("admin", "0614141.107346.2001", "test", base64stringimage, "test");

    }

What did I do wrong?

Thank you !

Derbie
  • 413
  • 1
  • 12
  • 28
  • There´s a chrome extension called Postman that could help you. Make the same call but from the browser to put aside WS errors on URL / params mapping. – reixa Jan 07 '14 at 09:56
  • By using that method, I reached a 413 error "file too large", since I am trying to send an image to the WS. – Derbie Jan 07 '14 at 10:26
  • I think that your problem resides inside your WS where the maximun bytes allowed to travel on a request is limited. Could you add more info about your WS in the question please. – reixa Jan 07 '14 at 10:32
  • I just paste the code of the WS, thanks for your help! – Derbie Jan 07 '14 at 12:58
  • Have you solved the problem? – reixa Jan 08 '14 at 10:13
  • No. Now I have a "code 400 bad request" when I send the request from postman plugin. The request sent is a simple post message containing the image, and I don't know if it is possible to have the details of the issue in the server log (the things I have on server side is very... light). Is it because of postman request format? – Derbie Jan 08 '14 at 13:51
  • check this link and try to implement uriTemplate and WebMessageBodyStyle.Bare http://stackoverflow.com/questions/3146329/400-bad-request-http-response-using-a-wcf-post-via-jquery – reixa Jan 08 '14 at 14:27
  • I have tried but no additional details. I changed the description of my problem accordingly and added the last updates I have done. – Derbie Jan 13 '14 at 10:07

1 Answers1

0

You should change the uploadReadAheadSize and maxReceivedMessageSize parameters on your server applicationHost.config file.

Here you´ve got a thread that talks about it

http://forums.iis.net/t/1169257.aspx?Request+Entity+Too+large+413+error+

This link might be useful too as it explains why you should change the uploadReadAheadSize and maxReceivedMessageSize parameters for better handling of file uploads.

http://www.codeproject.com/Articles/521725/413-Request-Entity-Too-Large

UPDATE

Try using this library for the http calls. It seems that you're sending a bad request to the server. I don't think it has something to do with the parameters that the ws expects but the http headers sent by the android app.

https://github.com/loopj/android-async-http

Hope it helps. :)

reixa
  • 6,903
  • 6
  • 49
  • 68