Android ICS using HttpURLConnection with https address cannot execute mail methods

I use this code with the lvl 4 API and it always worked before ICS. Since API level 14, when I use the https URL, this block of code always executes the GET method, not the POST method. If I use the http url, the code does POST and it all works. The exception that I get with lvl14 is FileNotFoundException. I don’t understand what is going on. Please help. Thank.

private byte[] Post(byte[] Header, byte[] Body, String protocol) throws IOException, MyAppConnectionException
{
    HttpURLConnection urlConnection = null;
    byte[] responseData = null;

    try
    {
        String url = MyApp.getContext().getResources().getString(R.string.ServerEndPoint);
        URL u = new URL(url);
        urlConnection = (HttpURLConnection) u.openConnection();
        urlConnection.setConnectTimeout(15000);
        urlConnection.setReadTimeout(45000);
        urlConnection.setRequestProperty("CONTENT-TYPE", protocol);
        urlConnection.setDoOutput(true); 
        urlConnection.setDoInput(true);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.connect();
    }
    catch(IOException ioex)
    {
        throw new MyAppConnectionException();
    }

    if(urlConnection != null)
    {
        DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());

        int msgLength = (int)(4 + Header.length + Body.length);

        outputStream.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(msgLength).array());
        outputStream.write(Header);
        outputStream.write(Body);
        outputStream.flush();
        outputStream.close();

        if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            DataInputStream inputStream = new DataInputStream(urlConnection.getInputStream());

            int dataLength = inputStream.available();
            byte[] msgbLength = new byte[4];
            inputStream.read(msgbLength, 0, 4);
            int length = ByteBuffer.wrap(msgbLength).order(ByteOrder.LITTLE_ENDIAN).getInt();

            assert(dataLength == length);      

            responseData = new byte[length - 4];
            inputStream.readFully(responseData, 0, length - 4);
            inputStream.close();
        }
        urlConnection.disconnect();
    }

    return responseData;}
+3
source share
1 answer

It looks like you are missing urlConnection.setRequestMethod("POST");.

http://developer.android.com/reference/java/net/HttpURLConnection.html#setRequestMethod(java.lang.String )

+1
source

All Articles