Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SMSGenerator {
- public static HttpURLConnection sendSingleSMS(HttpURLConnection lconnection, String username,
- String password, String senderId,
- String mobileNo, String message) {
- try {
- String smsservicetype = "singlemsg"; // For single message.
- String query = "username=" + URLEncoder.encode(username)
- + "&password=" + URLEncoder.encode(password)
- + "&smsservicetype=" + URLEncoder.encode(smsservicetype)
- + "&content=" + URLEncoder.encode(message) + "&mobileno="
- + URLEncoder.encode(mobileNo) + "&senderid="
- + URLEncoder.encode(senderId);
- lconnection.setRequestProperty("Content-length", String.valueOf(query.length()));
- lconnection.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- lconnection.setRequestProperty("User-Agent",
- "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
- // open up the output stream of the connection
- DataOutputStream output = new DataOutputStream(lconnection.getOutputStream());
- // write out the data
- int queryLength = query.length();
- output.writeBytes(query);
- // output.close();
- // get ready to read the response from the cgi script
- DataInputStream input = new DataInputStream(lconnection.getInputStream());
- // read in each character until end-of-stream is detected
- for (int c = input.read(); c != -1; c = input.read()) {
- System.out.print((char) c);
- }
- input.close();
- } catch (Exception e) {
- System.out.println("Something bad just happened.");
- e.printStackTrace();
- }
- return lconnection;
- }
- public static boolean sendSMS(String mobilenumber, String msg) throws MalformedURLException, IOException {
- boolean flag = false;
- Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxx", xx));
- URL surl = new URL("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
- HttpURLConnection connection = null;
- connection = (HttpURLConnection) surl.openConnection(proxy);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.setRequestMethod("POST");
- HttpURLConnection.setFollowRedirects(true);
- System.out.println("Response code " + connection.getResponseMessage());
- System.out.println("code : " + connection.getResponseCode() + " " + HttpURLConnection.HTTP_NOT_FOUND);
- if ("OK".equals(connection.getResponseMessage())) {
- System.out.println("in");
- SMSGenerator.sendSingleSMS(connection, "username", "password", "SMS", mobilenumber, msg);
- // System.out.println("Sent successful SMS");
- flag = true;
- } else if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("Hiiii");
- } else if (connection.getResponseCode() == HttpURLConnection.HTTP_BAD_GATEWAY) {
- System.out.println("Hi 1");
- } else if (connection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
- System.out.println("Hi 2");
- }
- // System.out.println("");
- return flag;
- }
- public static void main(String[] args) throws IOException {
- new SMSGenerator().sendSMS("xxxxxxxxxx", "ManojYest1");
- }
- }
- java.lang.IllegalStateException: Already connected
- at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3013)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement