6/25/10

SMS GateWay

Hi all , i had a requirement of implementing sms gateway in our application to send sms.  I worked on that and i would like to share that piece of code.
Depending on the SMS service provider api the implementation methodology may also change. Generally it can be implemented in two ways either using http service or smtp service.
I will provide the example of using http service.



void httpsTry() {
        HttpClient client = new HttpClient();
        //client.getParams().setParameter("http.useragent", "Test Client");

        BufferedReader br = null;

        PostMethod method = new PostMethod("api url");
        String to1[] = {"00000000", "00000000"};
             try {
            for (int i = 0; i < to1.length; i++) {
              
                method.addParameter("user", "\"test\"");
                method.addParameter("pass", "\"test\"");
                method.addParameter("phonenumber", "\"" + to1[i] + "\"");
                method.addParameter("subject", "\"test\"");
                method.addParameter("message", "\"test from dev team\"");
            
                int returnCode = client.executeMethod(method);

                if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                    System.err.println("The Post method is not implemented by this URI");
                    // still consume the response body
                    method.getResponseBodyAsString();
                } else {
                    br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
                    String readLine;
                    while (((readLine = br.readLine()) != null)) {
                        // System.err.println(readLine);
                        System.out.println("out put-->" + readLine);
                    }
                }
            }

        } catch (Exception e) {
            System.err.println(e);
        } finally {
            method.releaseConnection();
            if (br != null) {
                try {
                    br.close();
                } catch (Exception fe) {
                }
            }
        }

    }

No comments:

Post a Comment

Popular Posts