我想用struts发送手机号码短信
问题描述:
我在struts中工作,我正在使用eclipse,我正在发送手机号码上的短信和它的工作,但我无法获取未发送的消息,因为如果我在无效号码上发送消息,那么如何获得该号码无效的信息?每次显示200个响应代码。我想用struts发送手机号码短信
public class LessonAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
try
{
String message = " Greetings";
String username = "91886665555";
String password = "xyz";
String number = "0000000";
String requestUrl = "http://www.smszone.in/sendsms.asp?page=SendSmsBulk&"+
"username=" + URLEncoder.encode(username, "UTF-8")+
"&password=" + URLEncoder.encode(password, "UTF-8")+
"&number=" + URLEncoder.encode(number, "UTF-8")+
"&message=" + URLEncoder.encode(message, "UTF-8");
System.out.println(requestUrl);
URL url = new URL(requestUrl);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
System.out.println(uc.getResponseMessage());
System.out.println(uc.getResponseCode());
uc.setDoOutput(true);
OutputStream os= uc.getOutputStream();
StringBuilder r = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line;
while ((line = br.readLine()) != null)
r.append("status="+line);
br.close();
os.close();
System.out.println(r.toString());
uc.disconnect();
System.out.println("jignesh");
}
catch (Exception e)
{
System.out.println(e);
}
return mapping.findForward("success");
}
}
答
使用的标签,在<javascript>
文件.jsp
,写JS代码或jQuery代码实现Ajax功能。另一种方式是可以编写一些servlet的doGet功能,例如request.getParameter()
。显示你的代码,可以回答更多的问题。
答
public boolean sendSms() throws Exception
{
// Prepare Url
URLConnection myURLConnection = null;
URL myURL = null;
BufferedReader reader = null;
// encoding message
String encoded_message = URLEncoder.encode(message);
// Send SMS API
String mainUrl = "http://sms.hspsms.com/sendSMS?";
// Prepare parameter string
StringBuilder sbPostData = new StringBuilder(mainUrl);
sbPostData.append("username=" + username);
sbPostData.append("&message=" + encoded_message);
sbPostData.append("&sendername=" + sendername);
sbPostData.append("&smstype=" + smstype);
sbPostData.append("&numbers=" + numbers);
sbPostData.append("&apikey=" + apikey);
// final string
mainUrl = sbPostData.toString();
try
{
// prepare connection
myURL = new URL(mainUrl);
myURLConnection = myURL.openConnection();
myURLConnection.connect();
reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
// reading response
String response1;
while ((response1 = reader.readLine()) != null)
// print response
System.out.println(response1);
// finally close connection
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return true;
}