TC65 HTTP POST request

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    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();
            }
        }
    }
GD Star Rating
loading...
TC65 HTTP POST request, 10.0 out of 10 based on 2 ratings
Posted in English. Tags: , . 2 Comments »

2 Responses to “TC65 HTTP POST request”

  1. Paul Mansell Says:

    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

    GD Star Rating
    loading...
  2. Florent Clairambault Says:

    I don’t set a GSM timeout and I manually manage timeouts on TCP connections by sending keep-alive packets. I never use HTTP because it consumes too much bandwidth. I just posted this code to help people willing to use it, because someone asked me the question. I use the little protocol I talked about not that long ago : http://florent.clairambault.fr/the-m2mp-protocol

    GD Star Rating
    loading...

Leave a Reply