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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| private String sendPostHttpRequest( String url )
{
HttpConnection hcon = null;
DataInputStream dis = null;
DataOutputStream dos = null;
byte[] tab = null;
StringBuffer responseMessage = new StringBuffer();
// the request body
String params = null;
try {
// an HttpConnection with both read and write access
hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );
// set the request method to POST
hcon.setRequestMethod(HttpConnection.POST);
hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hcon.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT" );
//hcon.setRequestProperty("Content-Language", "en-US" );
hcon.setRequestProperty("Content-Language", "en-au" );
hcon.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
// obtain DataOutputStream for sending the request string
dos = hcon.openDataOutputStream();
params = "auth=" + _textField1.getString() + "&ID=" + _textField2.getString() + "&pda=NOK_123456&app=helloworld";
String t = _textField1.getString();
tab = t.getBytes();
//paramsEncrypted = encrypt(params); // -> auth=2&ID=2&PDA=OPL_234567&app=ifmmpxpsme...
//String encodedData = encode( params );
dos.write( params.getBytes() );
/*byte [] data = params.getBytes ();
for (int i = 0; i < data.length;i++)
{
dos.writeByte(data[i]);
}*/
dos.flush();
// obtain DataInputStream for receiving server response
dis = new DataInputStream( hcon.openInputStream() );
// retrieve the response from server
int ch;
while ((ch = dis.read()) != -1)
responseMessage.append((char)ch);
}
catch( Exception e )
{
e.printStackTrace();
responseMessage.append( "ERROR" );
}
finally
{
// free up i/o streams and http connection
try
{
if( dis != null ) dis.close();
if( dos != null ) dos.close();
if( hcon != null ) hcon.close();
} catch ( IOException ioe ) {
ioe.printStackTrace();
}//end try/catch
}//end try/catch/finally
//sresponseDecrypted = decrypt(...);
return ("parametre : " + params + " \n reponse serveur : " + responseMessage.toString() + "\n conversion" );
}//end sendHttpPost( String ) |