Developer Tools
BasicText SMSCampaignSendOTPVirtual NumberResellerSample CodePhonebookError Code
HOME LOGIN SIGN UP
  • Send SMS in PHP
  • Send SMS in Python
  • Send SMS in JAVA
  • Send SMS in JAVA For XML API
  • Send SMS in C#
  • Send SMS in Google Appscript
  • Send SMS in Windows 8
  • Send SMS in Android
  • Send SMS in iOS
  • Send SMS in VB6
  • Send SMS in Oracle
  • Send SMS in Go Language
  • Api Postman Collection

Java Sample Code Send SMS

Code:

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;

class Functions
{

	public static String hitUrl(String urlToHit, String param)
    	{
        	try
        	{
                URL url = new URL(urlToHit);
                HttpURLConnection http = (HttpURLConnection)url.openConnection();
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setRequestMethod("POST");

                DataOutputStream wr = new DataOutputStream(http.getOutputStream());
                wr.writeBytes(param);
                // use wr.write(param.getBytes("UTF-8")); for unicode message's instead of wr.writeBytes(param);

                wr.flush();
                wr.close();
                http.disconnect();	
  

                BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
                String inputLine;
                if ((inputLine = in.readLine()) != null)
                {
                        in.close();
                        return inputLine;
                }
                else
                {
                        in.close();
                        return "-1";
                }

            }
            catch(Exception e)
            {
                System.out.println("Exception Caught..!!!");
                e.printStackTrace();
                return "-2";
            }
        }
}

public class HitXmlData 
{
	public static void main(String[] args) 
	{
    		String strUrl = "http://api.msg91.com/api/postsms.php";
    		String xmlData = "data=
                                    <MESSAGE>
                                        <AUTHKEY>YOURAUTHKEY</AUTHKEY>
                                        <ROUTE>default</ROUTE>
                                        <SMS TEXT='message1 testing' FROM='AAAAAA'>
                                            <ADDRESS TO='9999999990'></ADDRESS>
                                        </SMS>
                                    </MESSAGE>"

		String output = Functions.hitUrl(strUrl, xmlData);
		System.out.println("Output is: "+output);

	}

}


© 2008-2025 | All Rights Reserved.