If you need to do a secured post operation with server we can use Authentication token from the server side that obtained when we log into system. And do the server side implementation to check the authentication token per each request coming from the user(client). Its better if we can delete that token periodically. And ask user to re login if that key is expired.Here in this post i will show how to send request with the authentication token. Remember that we need to set APN unless we configured device properly. Here is the separate post on how to Get APN key without worrying about the service provider that we used(Which contains most of service providers in united states and Canada). If you operator not available there add it following format. Later i will add server side code also.
<MCC,MNC>APN-KEY </MCC,MNC>
Here is the example
<310,160>wap.voicestream.com</310,160>
Here is the code for process post request to server
<MCC,MNC>
Here is the example
<310,160>wap.voicestream.com</310,160>
Here is the code for process post request to server
/**
* @param serviceUrl
* @param authToken
* @param requestData
* @return String
* @throws IOException
* @throws ServerException
* static String processPOSTRequest(String serviceUrl,String authToken, String requestData)
*/
public static String processPOSTRequest(String serviceUrl,String authToken, String requestData) throws IOException,ServerException {
HttpConnection httpConnection = null;
OutputStream streamOutput = null;
InputStream inputStream = null;
StringBuffer strBuffer = new StringBuffer();
String response = "";
try {
//Here getAPN() method gives you APN key for particular network operator
//I have written separate blog post on getting APN key for any operator
httpConnection = (HttpConnection) Connector.open(serviceUrl.concat(getAPN()), Connector.READ_WRITE);
httpConnection.setRequestMethod(HttpConnection.POST);
httpConnection.setRequestProperty("Content-Length", Integer.toString(requestData.length()));
httpConnection.setRequestProperty("Content-Type", "text/xml");
if (authToken.length() > 0) {
httpConnection.setRequestProperty("Authorization",Base64OutputStream.encodeAsString(authToken
.getBytes("UTF-8"), 0, authToken
.getBytes("UTF-8").length, false, false));
}
streamOutput = httpConnection.openOutputStream();
streamOutput.write(requestData.getBytes(), 0, requestData
.getBytes().length);
streamOutput.flush();
int responseCode = httpConnection.getResponseCode();
if (responseCode != 200) {
ServerException getErrorExceptions = new ServerException(
responseCode, httpConnection.getResponseMessage());
throw getErrorExceptions;
}
inputStream = httpConnection.openInputStream();
int character;
while (-1 != (character = inputStream.read())) {
strBuffer.append((char) character);
}
response = strBuffer.toString();
} finally {
try {
streamOutput.close();
inputStream.close();
httpConnection.close();
} catch (Exception e) {
//not related to user error if this exception happens due to
//unavailability then connection assigns to null and then implicitly removes it
}
httpConnection = null;
inputStream = null;
streamOutput = null;
}
return response;
}
No comments:
Post a Comment