Échec de l'authentification lors de l'envoi de SMS à l'aide de Way2Sms + Java


A essayé d'envoyer des SMS à partir de l'application Java en créant trois classes Java données ci-dessous 1. Pouvoirs 2. URLConnector 3. Way2Sms et l'exécution de la classe Way2Sms, mais l'authentification a échoué! en raison de responseCode est 404 ou 408 dans Way2Sms java méthode de connexion...toute aide pour résoudre ce serait apprécié....

**Way2Sms.java**
package way2sms;
/* Code from this site 
 * 
 * http://jtechbits.blogspot.in/2011/06/sending-sms-through-way2sms-in-java.html
 * 
 * */

import java.net.HttpURLConnection;
import java.util.Calendar;

public class Way2Sms 
{
    private static int responseCode = -1;
    private static String userCredentials;
    private static String cookie;
    private static String token;
    private static String site;
    private static String actionStr;
    private static String random1;
    private static String random2;
    private static String random3;
    private static Credentials credentials = new Credentials();

    public static void main(String[] args) 
    {
        login("mynumber", "mypassword");
        sendSMS("9663384741", "Hello");
        System.out.println("Message has been sent successfully!");
    }

    private static void setProxy(String host, int port) 
    {
        URLConnector.setProxy(host, port);  
    }

    private static void login(String uid, String pwd) 
    {
        getSite();
        preHome();
        String location = null;
        credentials.set("username", uid);
        credentials.append("password", pwd);
        credentials.append("userLogin", "no");
        credentials.append("button", "Login");
        userCredentials = credentials.getUserCredentials();
        URLConnector.connect("http://" + site + "/w2sauth.action", false, "POST", cookie, userCredentials);
        **responseCode = URLConnector.getResponseCode();**
        if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
        {
            exit("authentication failed!");
        }
        else
        {
            location = URLConnector.getLocation();
            URLConnector.disconnect();
            URLConnector.connect(location, false, "GET", cookie, null);
            responseCode = URLConnector.getResponseCode();
            if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
            {
                exit("redirection failed!");
                URLConnector.disconnect();
            }
        }
    }

    private static void sendSMS(String receiversMobNo, String msg) 
    {
        getActionString();
        credentials.reset();
        credentials.set("t_15_k_5", random1);
        credentials.append("i_m", "sn2sms");
        credentials.append("m_15_b", random2);
        if(actionStr != null)
            credentials.append("kriya", actionStr);
        else
        {
            exit("Action string missing!");
            credentials.append(random3, "");
            credentials.append(random1, token);
            credentials.append("catnamedis", "Birthday");
            credentials.append("chkall", "on");
            credentials.append("diffNo", Calendar.getInstance().getTime().toString().split(" ")[3]);
            credentials.append(random2, receiversMobNo);
            credentials.append("txtLen", "" + (140 - msg.length()));
            credentials.append("textArea", msg);
            userCredentials = credentials.getUserCredentials();
            URLConnector.connect("http://" + site + "/jsp/w2ssms.action", false, "POST", cookie + "; 12489smssending34908=67547valdsvsikerexzc435457", userCredentials);
            responseCode = URLConnector.getResponseCode();

            if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
                exit("sendSMS failed!");
            URLConnector.disconnect();
        }
    }

    private static void sendSMS(String[] receiversMobNos, String msg) 
    {
        int noOfReceivers = receiversMobNos.length;
        for(int i = 0; i < noOfReceivers; i++)
            sendSMS(receiversMobNos[i], msg);   
    }

    private static void getSite() 
    {
        URLConnector.connect("http://www.way2sms.com/", false, "GET", null, null);
        responseCode = URLConnector.getResponseCode();
        if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
            exit("getSite failed!");
        else
        {
            site = URLConnector.getLocation();
            if(site != null)
                site = site.substring(7, site.length() - 1);
        }
        URLConnector.disconnect();
    }

    private static void preHome() 
    {
        URLConnector.connect("http://" + site + "/content/prehome.jsp", false, "GET", null, null);
        responseCode = URLConnector.getResponseCode();
        if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
            exit("preHome failed");
        else
        {
            cookie = URLConnector.getCookie();
            token = cookie.substring(cookie.indexOf("~") + 1);
        }
        URLConnector.disconnect();
    }

    private static void getActionString() 
    {
        URLConnector.connect("http://" + site + "/jsp/SingleSMS.jsp?Token=" + token, false, "GET", cookie, null);
        responseCode = URLConnector.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_OK) 
        {
            String str = URLConnector.getResponse();
            String aStr = "id='kriya' value='";
            int index = str.indexOf(aStr);
            if(index > 0) 
            {
                index = index + aStr.length();
                StringBuffer actionStringChars = new StringBuffer();
                char ch = str.charAt(index);
                while(ch != '\'') 
                {
                    actionStringChars.append(ch);
                    ch = str.charAt(++index);
                }
                actionStr = actionStringChars.toString();
                getRandom1(str);
                getRandom2(str);
                getRandom3(str);
            }
        } 
        else
        {
            exit("getActionString failed!");
            URLConnector.disconnect();
        }
    }

    private static void getRandom1(String str) 
    { 
        String aStr = "id='t_15_k_5'"; 

        int index = str.indexOf(aStr); 
        if(index > 0) 
        { 
            index = index + aStr.length(); 

            StringBuffer random1Chars = new StringBuffer(); 
            char ch = str.charAt(index); 

            if(ch == '>') 
            { 
                ch = str.charAt(++index); 
                while(ch != '<') 
                { 
                    random1Chars.append(ch); 
                    ch = str.charAt(++index); 
                } 
            } 
            else 
            { 
                index += 8; 
                ch = str.charAt(index); 
                while(ch != '\'') 
                { 
                    random1Chars.append(ch); 
                    ch = str.charAt(++index); 
                } 
            } 
            random1 = random1Chars.toString(); 
        } 
    } 


    private static void getRandom2(String str) 
    { 
        String aStr = "id='m_15_b'"; 

        int index = str.indexOf(aStr); 
        if(index > 0) 
        { 
            index = index + aStr.length(); 
            StringBuffer random2Chars = new StringBuffer(); 
            char ch = str.charAt(index); 
            if(ch == '>') 
            { 
                ch = str.charAt(++index); 

                while(ch != '<') 
                { 
                    random2Chars.append(ch); 
                    ch = str.charAt(++index); 
                } 
            } 
            else 
            { 
                index += 8; 
                ch = str.charAt(index); 
                while(ch != '\'') 
                { 
                    random2Chars.append(ch); 
                    ch = str.charAt(++index); 
                } 
            } 
            random2 = random2Chars.toString(); 
        } 
    } 

    private static void getRandom3(String str) 
    { 
        String aStr = "dnipb"; 

        int index = str.lastIndexOf(aStr); 
        if(index > 0) 
        { 
            index -= 40; 

            StringBuffer random3Chars = new StringBuffer(); 
            char ch = str.charAt(index); 

            while(ch != '\"') 
            { 
                random3Chars.append(ch); 
                ch = str.charAt(--index); 
            } 
            random3 = random3Chars.reverse().toString(); 
        } 
    } 

    private static void exit(String errorMsg) 
    {
        System.err.println(errorMsg);
        System.exit(1);
    }
}

**URLConnector.java**

package way2sms;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;

public class URLConnector {
private static HttpURLConnection connection;
private static Proxy proxy;


public static void setProxy(String host, int port) {
proxy = new Proxy(Proxy.Type.HTTP, java.net.InetSocketAddress.createUnresolved(host, port));
}


public static void connect(String urlPath, boolean redirect, String method, String cookie, String credentials) 
{
    try 
    {
        URL url = new URL(urlPath);
        if(null != proxy)
            connection = (HttpURLConnection) url.openConnection(proxy);
        else
        {
            connection = (HttpURLConnection) url.openConnection();
            connection.setInstanceFollowRedirects(redirect);
            if(cookie != null)
                connection.setRequestProperty("Cookie", cookie);
            if(method != null && method.equalsIgnoreCase("POST")) 
            {
                connection.setRequestMethod(method);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            }
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.4) Gecko/20100101 Firefox/10.0.4");
            connection.setUseCaches (false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            if(credentials != null) 
            {
                DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
                wr.writeBytes (credentials);
                wr.flush ();
                wr.close ();
            }
        }

    }
    catch(Exception exception) 
    {
        System.out.println("Connection error");
    }
}

    public static String getCookie() 
    {
        String cookie = null;
        if(connection != null) 
        {
            String headerName=null;
            for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) 
            {
                if (headerName.equals("Set-Cookie")) 
                {
                    cookie = connection.getHeaderField(i).split(";")[0];
                    break;
                }
            }
        }
        return cookie;
    }

    public static String getLocation() 
    {
        String location = null;
        if(connection != null) 
        {
            String headerName=null;
            for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) 
            {
                if (headerName.equals("Location")) 
                {
                    location = connection.getHeaderField(i).split(";")[0];
                    break;
                }
            }
        }
        return location;
    }

    public static int getResponseCode() 
    {
        int responseCode = -1;
        if(connection != null)
        {
            try 
            {
                responseCode = connection.getResponseCode();
            } 
            catch(Exception exception) 
            {
                System.err.println("Response code error");
            }
        }
        return responseCode;
    }

    public static String getResponse() 
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if(connection != null) 
        {
            try 
            {
                InputStream is = connection.getInputStream();
                int next = is.read();
                while (next > -1) 
                {
                    bos.write(next);
                    next = is.read();
                }
                bos.flush();
            } 
            catch(Exception exception) 
            {
                System.err.println("Response error");
            }
        }
        return new String(bos.toByteArray());
    }
    public static String getErrorMessage() 
    {
        StringBuilder errorMessage = new StringBuilder();
        if(connection != null) 
        {
            try 
            {
                InputStream es = connection.getErrorStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(es));
                String line;
                while((line = rd.readLine()) != null) 
                {
                    errorMessage.append(line);
                    errorMessage.append("\r\n");
                }
                rd.close();
            } 
            catch(Exception exception) 
            {
                System.err.println("Error in getting error message");
            }
          }
          return errorMessage.toString();
    }
    public static void disconnect() 
    {
        if(connection != null)
            connection.disconnect();
    }
}
**Credentials.java**

package way2sms;

import java.net.URLEncoder;
import java.util.ArrayList;

public class Credentials 
{
    private ArrayList< String > list = new ArrayList< String >();
    public void set(String name, String value) {
    StringBuilder buffer = new StringBuilder();
    buffer.append(name);
    buffer.append("=");
    buffer.append(getUTF8String(value));
    add(buffer.toString());
}
    public void append(String name, String value) 
    {
        StringBuilder buffer = new StringBuilder();
        buffer.append("&");
        buffer.append(name);
        buffer.append("=");
        buffer.append(getUTF8String(value));
        add(buffer.toString()); 
    }

    private void add(String item) 
    {
        list.add(item);
    }

    private String getUTF8String(String value) 
    {
        String encodedValue = null;
        try 
        {
            encodedValue = URLEncoder.encode(value, "UTF-8");
        } 
        catch(Exception exception) 
        {
            System.err.println("Encoding error! Please try agian...");
            System.exit(1);
        }
        return encodedValue;
    }
    public boolean isEmpty() 
    {
        return list.isEmpty();
    }
    public void reset() 
    {
        list.clear();
    }
    public String getUserCredentials() 
    {
        StringBuilder buffer = new StringBuilder();
        int size = list.size();
        for(int i = 0; i < size; i++)
        {
            buffer.append(list.get(i));
        }
        return buffer.toString();
    }
}
Author: Vinoth Kumar Subramanian, 2015-01-18

1 answers

Ceci est dû au code de vérification humain qui est Captcha. Maintenant, vous ne pouvez pas vous connecter avec l'api précédente pour envoyer des SMS. Vous devez contacter votre fournisseur de services.

 0
Author: Altmish-E-Azam, 2015-01-18 16:38:48