Advertisement
Guest User

Untitled

a guest
Jul 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. public class SMSGenerator {
  2. public static HttpURLConnection sendSingleSMS(HttpURLConnection lconnection, String username,
  3. String password, String senderId,
  4. String mobileNo, String message) {
  5. try {
  6. String smsservicetype = "singlemsg"; // For single message.
  7. String query = "username=" + URLEncoder.encode(username)
  8. + "&password=" + URLEncoder.encode(password)
  9. + "&smsservicetype=" + URLEncoder.encode(smsservicetype)
  10. + "&content=" + URLEncoder.encode(message) + "&mobileno="
  11. + URLEncoder.encode(mobileNo) + "&senderid="
  12. + URLEncoder.encode(senderId);
  13.  
  14. lconnection.setRequestProperty("Content-length", String.valueOf(query.length()));
  15. lconnection.setRequestProperty("Content-Type",
  16. "application/x-www-form-urlencoded");
  17. lconnection.setRequestProperty("User-Agent",
  18. "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
  19.  
  20. // open up the output stream of the connection
  21. DataOutputStream output = new DataOutputStream(lconnection.getOutputStream());
  22.  
  23. // write out the data
  24. int queryLength = query.length();
  25. output.writeBytes(query);
  26. // output.close();
  27.  
  28. // get ready to read the response from the cgi script
  29. DataInputStream input = new DataInputStream(lconnection.getInputStream());
  30.  
  31. // read in each character until end-of-stream is detected
  32. for (int c = input.read(); c != -1; c = input.read()) {
  33. System.out.print((char) c);
  34. }
  35. input.close();
  36. } catch (Exception e) {
  37. System.out.println("Something bad just happened.");
  38. e.printStackTrace();
  39. }
  40. return lconnection;
  41. }
  42. public static boolean sendSMS(String mobilenumber, String msg) throws MalformedURLException, IOException {
  43. boolean flag = false;
  44. Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxx", xx));
  45. URL surl = new URL("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  46. HttpURLConnection connection = null;
  47. connection = (HttpURLConnection) surl.openConnection(proxy);
  48. connection.setDoInput(true);
  49. connection.setDoOutput(true);
  50. connection.setRequestMethod("POST");
  51. HttpURLConnection.setFollowRedirects(true);
  52. System.out.println("Response code " + connection.getResponseMessage());
  53. System.out.println("code : " + connection.getResponseCode() + " " + HttpURLConnection.HTTP_NOT_FOUND);
  54. if ("OK".equals(connection.getResponseMessage())) {
  55. System.out.println("in");
  56. SMSGenerator.sendSingleSMS(connection, "username", "password", "SMS", mobilenumber, msg);
  57. // System.out.println("Sent successful SMS");
  58. flag = true;
  59.  
  60. } else if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  61. System.out.println("Hiiii");
  62. } else if (connection.getResponseCode() == HttpURLConnection.HTTP_BAD_GATEWAY) {
  63. System.out.println("Hi 1");
  64. } else if (connection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
  65. System.out.println("Hi 2");
  66. }
  67. // System.out.println("");
  68. return flag;
  69. }
  70. public static void main(String[] args) throws IOException {
  71. new SMSGenerator().sendSMS("xxxxxxxxxx", "ManojYest1");
  72. }
  73. }
  74.  
  75. java.lang.IllegalStateException: Already connected
  76. at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3013)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement