2013-12-13 32 views
1

我需要通過我的Java應用程序發送短信。我有一段代碼曾經在我使用短信發送網站的地方工作得很好。然而,該網站引入了驗證碼驗證,因爲我的代碼失敗了。請找到我嘗試過的下面的代碼。請你引導我通過任何其他的替代品,我可以通過Java發送短信。如何使用java以編程方式發送SMS?

package com.test; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

public class Mobiletry2 
{ 
private final String LOGIN_URL = "http://*****.com/login.php"; 
private final String SEND_SMS_URL = "http://*****.com/home.php"; 
private final String LOGOUT_URL = "http://*****.com/logout.php?LogOut=1"; 

private final int MESSAGE_LENGTH = 10; 
private final int MOBILE_NUMBER_LENGTH = 140; 
private final int PASSWORD_LENGTH = 10; 

private String mobileNo; 
private String password; 
private DefaultHttpClient httpclient; 
Mobiletry2(String username,String password) 
{ 
this.mobileNo = username; 
this.password = password; 
httpclient = new DefaultHttpClient(); 
} 



public boolean isLoggedIn() throws IOException { 
// User Credentials on Login page are sent using POST 
// So create httpost object 
HttpPost httpost = new HttpPost(LOGIN_URL); 

// Add post variables to login url 
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
nvps.add(new BasicNameValuePair("MobileNoLogin", mobileNo)); 
nvps.add(new BasicNameValuePair("LoginPassword", password)); 
httpost.setEntity(new UrlEncodedFormEntity(nvps)); 

// Execute request 
HttpResponse response = this.httpclient.execute(httpost); 

//Check response entity 
HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      System.out.println("entity " + slurp(entity.getContent(), 10000000)); 
      System.out.println("entity " + response.getStatusLine().getStatusCode()); 

     return true; 
     } 
return false; 
} 

public boolean sendSMS(String toMobile,String message) throws IOException { 
HttpPost httpost = new HttpPost(SEND_SMS_URL); 
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
nvps.add(new BasicNameValuePair("MobileNos", toMobile)); 
       nvps.add(new BasicNameValuePair("Message", message)); 

       httpost.setEntity(new UrlEncodedFormEntity(nvps)); 
HttpResponse response = this.httpclient.execute(httpost); 
HttpEntity entity = response.getEntity(); 
if(entity != null) { 
         System.out.println("entity " + slurp(entity.getContent(), 10000000)); 
         System.out.println("entity " + response.getStatusLine().getStatusCode()); 
    return true; 
} 
return false; 
} 

public boolean logoutSMS() throws IOException { 
HttpGet httpGet = new HttpGet(LOGOUT_URL); 
HttpResponse response; 
response = this.httpclient.execute(httpGet); 
HttpEntity entity = response.getEntity(); 
if (entity != null) { 
    System.out 
    .println("entity " + slurp(entity.getContent(), 10000000)); 
    System.out.println("entity " 
    + response.getStatusLine().getStatusCode()); 
    return true; 
} 
return false; 
} 


public static String slurp(final InputStream is, final int bufferSize) 
{ 
    final char[] buffer = new char[bufferSize]; 
    final StringBuilder out = new StringBuilder(); 
    try { 
    final Reader in = new InputStreamReader(is, "UTF-8"); 
    try { 
     for (;;) { 
     int rsz = in.read(buffer, 0, buffer.length); 
     if (rsz < 0) 
      break; 
     out.append(buffer, 0, rsz); 
     } 
    } 
    finally { 
     in.close(); 
    } 
    } 
    catch (UnsupportedEncodingException ex) { 
    /* ... */ 
    } 
    catch (IOException ex) { 
     /* ... */ 
    } 
    return out.toString(); 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
//Replace DEMO_USERNAME with username of your account 
String username = "********"; 
//Replace DEMO_PASSWORD with password of your account 
String password = "****"; 
//Replace TARGET_MOBILE with a valid mobile number 
String toMobile = "****"; 

String toMessage = "Hello"; 

Mobiletry2 sMS = new Mobiletry2(username, password); 
try{ 
    if(sMS .isLoggedIn() && sMS .sendSMS(toMobile,toMessage)) 
    { 
    sMS.logoutSMS(); 
    System.out.println("Message was sent successfully "); 
    } 
} 
catch(IOException e) 
{ 
    System.out.println("Unable to send message, possible cause: " + e.getMessage()); 
} 
} 
} 
+0

您是否嘗試過[twilio](http://www.twilio.com) – SpringLearner

+0

allonsms.com是否有另一個API用於機器?也許是SOAP或REST API,甚至是SMPP或CIMD2 API(在所有情況下都會涉及到一些學習)。有許多提供商,但通常他們希望您展示廣告或獲得付費賬戶。 –

+0

所以,你說你濫用了一項服務而沒有付費,他們引入了一種意思來阻止它,現在你希望我們幫你規避這種保護? – DarkDust

回答

0

然後通過sms providing API去,美使用。他們必須提供任何備用的web服務來過這個問題。 Captcha用於防止任何網站上的自動提交。

0

驗證碼在運行時生成,所以無法在您的應用程序中預定義驗證碼,也就是說驗證碼出現在圖片中以防止任何網站上的自動提交。

相關問題