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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| package Outils.IO;
import Outils.Gui.GuiUserCom;
import Outils.Str.Str;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
/**
*
*/
// ============================================================================================================================================================
public class SmsFactor
// ============================================================================================================================================================
{
public static String statusOk = "Delivered";
public static String envoiSms(String gsm, String text){
String résultat = "";
String résultatCourt = "";
String query = "";
String contentXML ="";
try
{
// l'adresse email de votre compte sur www.smsfactor.com
String username = "LOGIN"; // Options.smsLogin;
// Mot de passe de votre compte www.smsfactor.com et correspondant au login
String password = "MP"; // Crypto.decrypte(Options.staticKeyData, Options.smsPass);
// Expéditeur du message tel qu'il apparaitra sur le téléphone.
String sender = "GenAmiga"; //JAVA " + Options.entreprise.getNom();
// Message de votre sms
// Numéro de téléphone correspondant au destinataire du message
// pour les tests
//gsm = "33601020304";
//text = "Test";
//
// Str.printString(username, "username");
// Str.printString(password, "password");
// Str.printString(text, "Text");
// Str.println("");
// Encodage et envoi du flux
// basé sur l'exemple Java
//contentXML ="<sms><authentification><username>"+username+"</username><password>"+password+"</password></authentification><message><sender>"+sender+"</sender><text>"+text+"</text></message><recipients><gsm>"+gsm+"</gsm></recipients></sms>";
// basé sur la documentation 3.1 du 19 avril 2015
contentXML ="XML=" +
"<sms>" +
"<authentication>" +
"<username>LOGIN</username>" +
"<password>MP</password>" +
"</authentication>" +
"<message>" +
"<sender>SenderTEST</sender>" +
"<text>Message TEST</text>" +
"</message>" +
"<recipients>" +
"<gsm gsmsmsid=\"100\">33601020304</gsm>" +
"</recipients>" +
"</sms>";
System.out.println("Message à envoyer : "+contentXML);
Str.println("");
boolean encodeUTF8 = false;
if (encodeUTF8){
query = URLEncoder.encode(contentXML, "UTF-8");
System.out.println("Message encodé en UTF-8: "+query);
Str.println("");
}else{
query = contentXML;
Str.println("Message:" + query);
Str.println("");
}
URLConnection urlConnection = new URL("https://secure.smsfactor.com/API/apiV2.php").openConnection();
// URLConnection urlConnection = new URL("https://api.smsfactor.com").openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
urlConnection.setRequestProperty("accept-charset", "UTF-8");
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
writer.write(query); // Write POST query string (if any needed).
} finally {
if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}
// Récupération de la réponse
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Writer stringWriter = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
stringWriter.write(buffer, 0, n);
}
} finally {
inputStream.close();
}
System.out.println("réponse : "+stringWriter.toString());
résultat = stringWriter.toString();
DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document parse = newDocumentBuilder.parse(new ByteArrayInputStream(résultat.getBytes()));
// System.out.println(parse.getFirstChild().getTextContent());
résultatCourt = Str.stringNettoyéeDesEspacesEnTrop(parse.getFirstChild().getTextContent().split("\n")[2]);
Str.println("résultatCourt = " + résultatCourt);
}
} catch (IOException | ParserConfigurationException | SAXException | DOMException ex) {
GuiUserCom.erreur(ex);
}
return résultatCourt;
}
public static void main(String args[]) throws Exception {
SmsFactor.envoiSms("33601020304", "TEST");
}
} |
Partager