1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
*
* @author Mo
*/
public class EnvoiMessage extends HttpServlet {
@Override
protected void doGet(HttpServletRequest requete, HttpServletResponse reponse)
throws ServletException, IOException {
doPost(requete , reponse);
}
@Override
protected void doPost(HttpServletRequest requete, HttpServletResponse reponse)
throws ServletException, IOException {
reponse.setContentType("text/html");
String phoneNumber = requete.getParameter("telephone");
String message = requete.getParameter("message");
System.out.println(phoneNumber);
System.out.println(message);
String textMessage = URLEncoder.encode(message, "UTF-8");
String apiAccessKey = "a1234b56789";
String url = "http://sms.alpha.orange-api.net/sms/sendSMS.xml?id="
+ apiAccessKey + "&to=" + phoneNumber + "&content="
+ textMessage;
// Send GET request
URL client = new URL(url);
URLConnection conn = client.openConnection();
InputStream responseBody = conn.getInputStream();
// Convert in XML document
Document response = null;
try
{
response = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(responseBody);
}
catch (ParserConfigurationException ex)
{
}
catch (SAXException ex)
{
}
responseBody.close();
// Xpath expression to get the status
XPath xPath = XPathFactory.newInstance().newXPath();
String status;
try {
status = xPath.evaluate("/response/status/status_msg", response);
System.out.println("Status: " + status);
}
catch (XPathExpressionException ex)
{
}
// display status
}
} |
Partager