TC65 HTTP POST request

This is just a quick code to show how to do a simple http POST request from a TC65 chip.

    private void httpCheck() {
        String data = "Parameter1=000009960623185&Parameter2=$GPWPL,4807.038,N,01131.000,E,WPTNME*5C";
        String url = "http://test.webingenia.com/postIdent";
        HttpConnection conn = null;
        InputStream is = null;
        OutputStream os = null;
        try { // Test HTTP connection

            // We prepare the POST request
            conn = (HttpConnection) Connector.open( url );
            conn.setRequestMethod( HttpConnection.POST );
            conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
            os = conn.openOutputStream();
            os.write( data.getBytes() );

            // We launch the request
            System.out.println( "Response code : " + conn.getResponseCode() );

            // We display the generated content
            is = conn.openInputStream();
            int ch;
            System.out.println( "Output : " );
            while ( (ch = is.read()) != -1 )
                System.out.print( (char) ch );

        } catch ( Exception ex ) {
            System.out.println( "Http : ex : " + ex.getClass() + " : " + ex.getMessage() );
            ex.printStackTrace();
        } // Whatever happens, we close everything
        finally {
            try {
                if ( conn != null )
                    conn.close();
                if ( is != null )
                    is.close();
                if ( os != null )
                    os.close();
            } catch ( Exception ex ) {
                System.out.println( "Http : ex2 : " + ex.getClass() + " : " + ex.getMessage() );
                ex.printStackTrace();
            }
        }
    }

13 thoughts on “TC65 HTTP POST request”

  1. Hi,

    I am using the a TC65 and TC65i – how do you recommend using network timeouts ? or are network timeouts handled authomajically ?

    I mean – if the server is not responding within x seconds ? or the server is busy ? would you check the response code ? ensure its HTTP_OK ?

    Any advice is welcome

    Thanks

  2. Hi,
    I´m trying this sample but I got a class java.io.IOException Profile could not be activated writing the data on the output stream…

    Any idea about this,
    Thanks in advance

  3. It means the DNS server you specified or the APN given automatically by your GPRS network are probably wrong. It might mean you are not using a valid APN. You need to specify the correct GPRS settings using the “AT^SJNET=***” AT command.

  4. Hi,

    I’m trying to do a HttpConnection in my TC65 but i keep getting the error “Could not resolve hostname” although i tried with URLs accessible from my laptop and even your’s. Do you have any advice to help fix this?

    Has anything to do with the APN?
    I would greatly appreciate any help.

    Thanks in advance,
    Hugo

  5. It’s a very basic and common error: The hostname cannot be resolved, it means you DNS aren’t specified, weren’t given by your provider or couldn’t be contacted.
    Maybe your GPRS connection doesn’t work at all.

    You should try using an IP address to start making a proper diagnosis.

  6. Thanks for your answer.
    Yes, for some reason the default DNS wasn’t working, if i use the IP I don’t get that error.
    Although I have another problem now. Every time I try to access any page the getResponseCode() method launches an exception saying “An unexpected error occurred”.

    Do you have any ideia why this happens or how to solve it?
    Again i would greatly appreciate any help.

    Thanks!
    Hugo

  7. Hi,

    I’m trying to test this, but I can’t get it to work properly. It usually blocks at conn.getResponseCode(). From time to time it works, but most of the time it jams there. No error, no nothing. The server works and responds properly.

    Thanks,
    Tudor

    1. Hi Tudor,

      I was never faced with this issue. Maybe someone at the javacint group might be of some help.

      Your problem seems rather odd though. It should never actually block here, it should at least throw a timeout exception.

      Best regards,
      Florent

  8. Hi,

    Thank you for the answer. Yes, it’s very odd. One in like 10-20 tries it works ok. Anyway, I’ll try the javacint group and if I manage to fix it, I’ll post the answer here, too.

    Thank you

  9. Errata corrige:

    When I connect with automatic dns I have NO problem.
    If I use the company DNS i get “could not resolve hostname”.
    If I check the condition “could not resolve hostname” and then retry to connect with automatic dns, I get another time “could not resolve hostname”… why?

    Ex:
    It work (second parameter is DNS address):
    connectGPRS( destAddress, “0.0.0.0″ );

    It NOT work (second parameter is DNS address)::
    if( connectGPRS( destAddress, “x.y.w.z” ) == false )
    connectGPRS( destAddress, “0.0.0.0″ );

    My question is: can I retry a new connection with another DNS after a failed connection ?

    Thanks in advance!
    Stefano

  10. Hey florent nice blog,
    can i ask you how you configured your modem prior to using this code, i keep having profile could not be activated errors.
    Thanks-Atef

Leave a Reply

Your email address will not be published. Required fields are marked *