Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用免费短信网关发送免费短信_Java_Sms Gateway - Fatal编程技术网

在java中使用免费短信网关发送免费短信

在java中使用免费短信网关发送免费短信,java,sms-gateway,Java,Sms Gateway,我正在尝试使用我的java应用程序发送免费短信。 我正在检查way2SMS,但它没有成功。没有显示错误。是否有其他可用的可用免费网关?下面是我的示例代码 import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.logging.Leve

我正在尝试使用我的java应用程序发送免费短信。 我正在检查way2SMS,但它没有成功。没有显示错误。是否有其他可用的可用免费网关?下面是我的示例代码

    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.message.BasicNameValuePair;
    import org.sms.jfreesms.SMS;
    import org.sms.jfreesms.exception.NotAuthenticatedException;


public class Way2SMS implements SMS {

private HttpClient smsClient = null;
private String jid = "";

private boolean authenticated = false;

private boolean debug = false;

public Way2SMS() {
    ClientConnectionManager manager = new ThreadSafeClientConnManager();
    smsClient = new DefaultHttpClient(manager);
}

@Override
public boolean login(String userName, String password) {

    authenticated = true;

    smsClient.getParams().setParameter("Host", "site4.way2sms.com");
    smsClient.getParams().setParameter("Connection", "keep-alive");
    smsClient.getParams().setParameter("Cache-Control", "max-age=0");
    smsClient.getParams().setParameter("Origin", "http://site4.way2sms.com");
    smsClient.getParams().setParameter("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
    smsClient.getParams().setParameter("Content-Type", "application/x-www-form-urlencoded");
    smsClient.getParams().setParameter("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    smsClient.getParams().setParameter("Accept-Encoding", "gzip,deflate,sdch");
    smsClient.getParams().setParameter("Accept-Language", "en-US,en;q=0.8");
    smsClient.getParams().setParameter("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    smsClient.getParams().setParameter("Cookie", "__gads=ID=c9b227532a00044e:T=1334849437:S=ALNI_MaWzQCpmJ7x1Bv36O4X6aln2uyyOw; JSESSIONID=A03~EAC1C01D2271FF74DBF7FC0BAF236370.w803");

    HttpPost post = new HttpPost("http://site4.way2sms.com/Login1.action");

    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("username", userName));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }

    // Execute HTTP Post Request
    HttpResponse response = null;
    try {
        response = smsClient.execute(post);
        //System.out.println(response);
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    for (Header header : response.getAllHeaders()) {
        if (header.getName().equalsIgnoreCase("Set-Cookie")) {
            String value = header.getValue();
            jid = value.substring(value.indexOf("=") + 1, value.indexOf(";"));
            //System.out.println(jid);
        }
        if (header.getName().equalsIgnoreCase("Location")) {
            String value = header.getValue();
            if(value.endsWith("id="))
            {
                authenticated = false;
            }
            //System.out.println(jid);
        }
    }

    if (response.getEntity() != null) {
        try {
            response.getEntity().consumeContent();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    if(debug == true)
    {
        if(authenticated == true)
        {
            System.out.println("Logged in successfully.");
        }
        else
        {
            System.out.println("Failed to login. Please try again");
        }
    }
    return authenticated;
}

@Override
public void send(String mobileNo, String msg)throws NotAuthenticatedException {

    if(isAuthenticated() == false)
    {
        throw new NotAuthenticatedException("You are not Authenticated.");
    }

    smsClient.getParams().setParameter("Host", "site4.way2sms.com");
    smsClient.getParams().setParameter("Connection", "keep-alive");
    smsClient.getParams().setParameter("Cache-Control", "max-age=0");
    smsClient.getParams().setParameter("Origin", "http://site4.way2sms.com");
    smsClient.getParams().setParameter("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
    smsClient.getParams().setParameter("Content-Type", "application/x-www-form-urlencoded");
    smsClient.getParams().setParameter("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    smsClient.getParams().setParameter("Accept-Encoding", "gzip,deflate,sdch");
    smsClient.getParams().setParameter("Accept-Language", "en-US,en;q=0.8");
    smsClient.getParams().setParameter("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    smsClient.getParams().setParameter("Cookie", "__gads=ID=c9b227532a00044e:T=1334849437:S=ALNI_MaWzQCpmJ7x1Bv36O4X6aln2uyyOw; JSESSIONID=" + jid);

    HttpPost post = new HttpPost("http://site4.way2sms.com/quicksms.action");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("HiddenAction", "instantsms"));
    nameValuePairs.add(new BasicNameValuePair("catnamedis", "Birthday"));
    nameValuePairs.add(new BasicNameValuePair("Action", "sf55sa5655sdf5"));
    nameValuePairs.add(new BasicNameValuePair("chkall", "on"));
    nameValuePairs.add(new BasicNameValuePair("MobNo", mobileNo));
    nameValuePairs.add(new BasicNameValuePair("textArea", msg));
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }
    try {
        // Execute HTTP Post Request
        HttpResponse response = smsClient.execute(post);
        //System.out.println(response);
        if (response.getEntity() != null) {
            try {
                response.getEntity().consumeContent();
                //EntityUtils.consume(response.getEntity());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    if(debug == true)
    {
        System.out.println("Message sent Successfully.");
    }
}

@Override
public boolean isAuthenticated() {
    return authenticated;
}

@Override
public void setDebug(boolean debug) {
    this.debug = debug;
}

}

提供免费短信的企业没有可持续的商业模式。它们不会持久。因此,依赖免费短信的应用程序也不可持续。HttpEntity.consumeContent不推荐使用。但是在用适当的方法EntityUtils.consumeentity替换它之前,您可能需要实际显示响应的标题和内容。他们可能会给你一个线索,告诉你遗漏了什么。有什么可以提供的服务吗?看到这个了吗