Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Error in URL connection

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in URL connection

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.Proxy;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;

    /**
    *
    * @author sreenath
    */
    public class Way2SMS {
    private URLConnection sendSMSConnection;
    private String sessionCookie;
    private Proxy proxy;

    //Function to support connection through an HTTP Proxy
    public void setProxy(String proxyHost,int proxyPort)
    {
    proxy=new Proxy(Proxy.Type.HTTP,java.net.InetSocketAddress.c reateUnresolved(proxyHost, proxyPort));
    }
    //Logging in to Way2sms and returning the authentication cookie
    //No need to Give the cookie back to sendSMS() but cookie is returned for expanding the flexibility of the code



    /*public String loginWay2SMS(String userName,String password)
    {
    String cookie=null;
    URL urlLogin;
    String loginContent;
    HttpURLConnection loginConnection;
    if(userName==null || userName.isEmpty())
    {
    System.err.println("A Valid User Name must be present!");
    System.exit(0);
    }
    if(password==null || password.isEmpty())
    {
    System.err.println("A Valid Password must be present!");
    System.exit(0);
    }
    try {
    //UTF-8 encoding is the web standard so data must be encoded to UTF-8
    userName=URLEncoder.encode(userName, "UTF-8");
    password=URLEncoder.encode(password, "UTF-8");
    urlLogin=new URL("http://site5.way2sms.com/auth.cl");
    if(proxy==null)
    {
    loginConnection = (HttpURLConnection) urlLogin.openConnection();
    }
    else
    {
    loginConnection = (HttpURLConnection) urlLogin.openConnection(proxy);
    }

    loginContent="username=" + userName + "&password=" + password;
    //Faking that we are from a valid client
    loginConnection.setDoOutput(true);
    loginConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
    loginConnection.setRequestProperty("Content-Length", String.valueOf(loginContent.length()));
    loginConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    loginConnection.setRequestProperty("Accept", "*/*");
    loginConnection.setRequestProperty("Referer", "http://site5.way2sms.com//entry.jsp");
    loginConnection.setRequestMethod("POST");
    loginConnection.setInstanceFollowRedirects(false);
    //Writing the Content to the site
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(loginConnection.getOutputStream ()), true);
    printWriter.print(loginContent);
    printWriter.flush();
    printWriter.close();
    //Reading the cookie
    cookie = loginConnection.getHeaderField("Set-Cookie");


    } catch (MalformedURLException ex) {
    System.err.println("Login URL Error");
    System.exit(0);
    } catch (UnsupportedEncodingException ex) {
    System.err.println("Error in encoding Username or Password");
    System.exit(0);
    }catch (IOException ex) {
    System.err.println("Can not connect to Login URL");
    System.exit(0);
    }
    if(cookie==null || cookie.isEmpty())
    {
    System.err.println("Some error occured...Try again in a few seconds..If still problem exists check your username and password");
    }
    sessionCookie=cookie;
    return cookie;

    }*/



    public void sendSMS(String phoneNumber,String message,String action)
    {

    if(phoneNumber==null || phoneNumber.isEmpty())
    {
    System.err.println("Enter A Valid Phone Number");
    System.exit(0);
    }
    else
    {
    try
    {

    long testLong=Long.valueOf(phoneNumber);
    }catch(NumberFormatException ex)
    {
    System.err.println("Invalid Phone Number");
    System.exit(0);
    }


    }

    if(message==null || message.isEmpty())
    {
    System.err.println("Enter A Valid Phone Number");
    System.exit(0);
    }
    else if(message.length()>140)
    {
    System.err.println("Message should be less than 140 characters");
    }
    if(action==null || action.isEmpty())
    {
    System.err.println("Enter Valid Action to send Message");
    System.exit(0);
    }

    URL sendURL;
    HttpURLConnection sendConnection;
    String sendContent;
    try {
    message=URLEncoder.encode(message, "UTF-8");
    sendURL=new URL("http://site5.way2sms.com/FirstServletsms?custid=");
    if(proxy==null)
    {
    sendConnection = (HttpURLConnection) sendURL.openConnection();
    }
    else
    {
    sendConnection = (HttpURLConnection) sendURL.openConnection(proxy);
    }
    sendContent="custid=undefined&HiddenAction=instant sms&Action="+action+"&login=&pass=&MobNo="+ phoneNumber+ "&textArea="+message;
    sendConnection.setDoOutput(true);
    sendConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
    sendConnection.setRequestProperty("Content-Length", String.valueOf(sendContent.getBytes().length));
    sendConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    sendConnection.setRequestProperty("Accept", "*/*");
    sendConnection.setRequestProperty("Cookie", sessionCookie);
    sendConnection.setRequestMethod("POST");
    sendConnection.setInstanceFollowRedirects(false);

    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(sendConnection.getOutputStream( )),true);
    printWriter.print(sendContent);
    printWriter.flush();
    printWriter.close();
    //Reading the returned web page to analyse whether the operation was sucessfull
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sendConnection.getInputStream()) );
    StringBuilder SendResult=new StringBuilder();
    String line;
    while ((line=bufferedReader.readLine()) != null)
    {
    SendResult.append(line);
    SendResult.append('\n');
    //Message has been submitted successfully
    }
    if(SendResult.toString().contains("Message has been submitted successfully"))
    {
    System.out.println("Message sent to "+phoneNumber+" successfully.");
    }
    else
    {
    System.err.println("Message could not send to "+phoneNumber+". Also check login credentials");
    }
    bufferedReader.close();

    }catch (UnsupportedEncodingException ex) {
    System.err.println("Message content encoding error");
    System.exit(0);
    }catch (MalformedURLException ex) {
    System.err.println("Sending URL Error");
    System.exit(0);
    }catch (IOException ex) {
    System.err.println("Sending URL Connection Error");
    System.exit(0);
    }


    }
    public void logoutWay2SMS()
    {
    try {
    HttpURLConnection logoutConnection;
    URL logoutURL;
    logoutURL = new URL("http://site5.way2sms.com/jsp/logout.jsp");
    if(proxy==null)
    {
    logoutConnection = (HttpURLConnection) logoutURL.openConnection();
    }
    else
    {
    logoutConnection = (HttpURLConnection) logoutURL.openConnection(proxy);
    }

    logoutConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
    logoutConnection.setRequestProperty("Accept", "*/*");
    logoutConnection.setRequestProperty("Cookie", sessionCookie);
    logoutConnection.setRequestMethod("GET");
    logoutConnection.setInstanceFollowRedirects(false) ;
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(logoutConnection.getInputStream( )));
    while ((bufferedReader.readLine()) != null);
    bufferedReader.close();
    } catch (MalformedURLException ex) {
    System.err.println("Logout URL Error");
    System.exit(0);
    }catch (IOException ex) {
    System.err.println("Logout URL Connection Error");
    System.exit(0);
    }
    }
    Way2SMS()
    {
    proxy=null;
    sendSMSConnection=null;
    }
    public static void main(String args[])
    {
    final String USERNAME="";//REQUIRED
    final String PASSWORD="";//REQUIRED
    final String ACTION="";//REQUIRED : In order to understand ACTION value please read the blog
    Way2SMS sms=new Way2SMS();
    //HTTP PROXY
    //sms.setProxy("10.1.1.1",8080); //REQUIRED ONLY IF CONNECTING THROUGH A PROXY

    StringBuilder phoneNumber= new StringBuilder();
    StringBuilder message=new StringBuilder();
    if(args.length>0)
    {
    if(args[0].toLowerCase().compareTo("phone")==0)
    {
    int i=1;
    while(args[i].toLowerCase().compareTo("message")!=0)
    {
    phoneNumber.append(args[i]);
    phoneNumber.append(';');
    i++;
    }
    for(i=i+1;i<args.length;i++)
    {
    message.append(args[i]);
    message.append(' ');
    }
    }
    else
    {
    System.out.println("USAGE : Way2SMS phone <phonenumber1> <phonenumber2> ... message <message>");
    System.exit(0);
    }
    }
    else
    {
    System.out.println("USAGE : Way2SMS phone <phonenumber1> <phonenumber2> ... message <message>");
    System.exit(0);
    }



    String cookie=sms.loginWay2SMS(USERNAME,PASSWORD);
    String textMessage=message.toString();
    String strPhoneNumber=phoneNumber.toString();
    String arrPhoneNUmber[]=strPhoneNumber.split(";");
    for(int i=0;i<arrPhoneNUmber.length;i++)
    {

    sms.sendSMS(arrPhoneNUmber[i], textMessage, ACTION);
    }

    sms.logoutWay2SMS();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error in URL connection

    You forgot to post the full text of the error message.
    Also please Edit your post and wrap your code with
    [code=java]<YOUR CODE HERE>[/code] to get highlighting

Similar Threads

  1. connection
    By sugukrish in forum JDBC & Databases
    Replies: 2
    Last Post: November 5th, 2011, 11:14 PM
  2. Connection Time Out
    By devett in forum Java Networking
    Replies: 3
    Last Post: September 26th, 2011, 08:05 AM
  3. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM
  4. JDBC connection error
    By nrao in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 11th, 2010, 12:43 PM
  5. Thread and connection
    By Param in forum Threads
    Replies: 0
    Last Post: April 26th, 2010, 03:43 AM