Created
January 28, 2014 01:23
-
-
Save oshanz/8660798 to your computer and use it in GitHub Desktop.
java post request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package send_sms; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.math.BigInteger; | |
import java.net.URL; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.ResourceBundle; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.PasswordField; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TextField; | |
import javafx.stage.Stage; | |
import javax.swing.JOptionPane; | |
import org.apache.http.HttpHost; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.config.RequestConfig; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.ini4j.Wini; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
/** | |
* | |
* @author trisquel | |
*/ | |
public class FXMLDocumentController implements Initializable { | |
@FXML | |
private TextField tele; | |
@FXML | |
private PasswordField pass; | |
@FXML | |
private TextArea msg; | |
@FXML | |
private Button btn; | |
private Stage stage; | |
private List<NameValuePair> nameValuePairs; | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
btn.setDisable(true); | |
tele.textProperty().addListener(new ChangeListener<String>() { | |
@Override | |
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { | |
if (newValue == null || newValue.isEmpty()) { | |
btn.setDisable(true); | |
} else { | |
if (newValue.length() > 10) { | |
tele.setText(oldValue); | |
return; | |
} | |
if (newValue.matches("^[0]*$")) { | |
tele.setText(oldValue); | |
return; | |
} | |
if (newValue.matches("^[0-9]{9}$")) { | |
btn.setDisable(false); | |
} else { | |
btn.setDisable(true); | |
} | |
} | |
} | |
}); | |
} | |
@FXML | |
public void sendSMS(ActionEvent event) throws JSONException { | |
try { | |
String message = msg.getText(); | |
String telephone = tele.getText(); | |
String password = pass.getText(); | |
MessageDigest md5 = MessageDigest.getInstance("MD5"); | |
md5.update(password.getBytes(), 0, password.length()); | |
String signature = new BigInteger(1, md5.digest()).toString(16); | |
SimpleDateFormat date = new SimpleDateFormat("MM-dd-yyyy"); | |
SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss"); | |
Date myDate = new Date(); | |
File file = new File("config.ini"); | |
file.createNewFile(); | |
Wini ini = new Wini(file); | |
if (ini.isEmpty()) { | |
ini.add("configuration", "remoteIP", "http://gateway.ceylonlinux.com/sms/sms"); | |
ini.store(); | |
} | |
String url = ini.get("configuration", "remoteIP"); | |
// url = "http://localhost/excelread/sms"; | |
nameValuePairs = new ArrayList<>(); | |
//{"clientID":"c001","token":"29fe5623a02b5ebcf38a6266824b5a5e","PhoneNumber":"94772189584","Message":"Hello","Date":"2013-01-09","Time":"10:30"} | |
nameValuePairs.add(new BasicNameValuePair("clientID", "testApp")); | |
nameValuePairs.add(new BasicNameValuePair("token", signature)); | |
nameValuePairs.add(new BasicNameValuePair("PhoneNumber", telephone)); | |
nameValuePairs.add(new BasicNameValuePair("Message", message)); | |
nameValuePairs.add(new BasicNameValuePair("Date", date.format(myDate))); | |
nameValuePairs.add(new BasicNameValuePair("Time", time.format(myDate))); | |
CloseableHttpClient httpclient = HttpClients.createDefault(); | |
try { | |
HttpHost target = new HttpHost(url); | |
// HttpHost proxy = new HttpHost("192.168.1.1", 3128); | |
// RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); | |
HttpHost direct = new HttpHost("106.187.96.196"); | |
RequestConfig config = RequestConfig.custom().setProxy(direct).build(); | |
HttpPost request = new HttpPost(url); | |
request.setConfig(config); | |
request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); | |
// System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); | |
try (CloseableHttpResponse response = httpclient.execute(target, request)) { | |
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); | |
String line = ""; | |
StringBuilder sb = new StringBuilder(); | |
while ((line = rd.readLine()) != null) { | |
sb.append(line); | |
} | |
JSONObject jo = new JSONObject(sb.toString()); | |
int status = jo.getInt("status"); | |
if (status == 100) { | |
JOptionPane.showMessageDialog(null, "successful"); | |
// Dialogs.showInformationDialog(stage, "successful"); | |
} else if (status == -100) { | |
JOptionPane.showMessageDialog(null, "Server Error", "Server Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Server Error", "Server Error", "Error", Dialogs.DialogOptions.OK); | |
} else if (status == -200) { | |
JOptionPane.showMessageDialog(null, "Invalid Password", "Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Invalid Password", "Error", "Error", Dialogs.DialogOptions.OK); | |
} else if (status == -300) { | |
JOptionPane.showMessageDialog(null, "Please Fill Empty Fields", "Field Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Please Fill Empty Fields", "Field Error", "Error", Dialogs.DialogOptions.OK); | |
} else if (status == -400) { | |
JOptionPane.showMessageDialog(null, "Application parameter Error", "Application Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Application parameter Error", "Application Error", "Error", Dialogs.DialogOptions.OK); | |
} else if (status == -500) { | |
JOptionPane.showMessageDialog(null, "Message Repeated", "Application Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Message may Repeat", "Server Error", "Error", Dialogs.DialogOptions.OK); | |
} else { | |
JOptionPane.showMessageDialog(null, "Status " + status, "Server Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, "Status " + status, "status", "Error", Dialogs.DialogOptions.OK); | |
} | |
} | |
} catch (IOException ex) { | |
JOptionPane.showMessageDialog(null, ex.getMessage(), "Server Error", JOptionPane.WARNING_MESSAGE); | |
} finally { | |
try { | |
httpclient.close(); | |
} catch (IOException ex) { | |
JOptionPane.showMessageDialog(null, ex.getMessage(), "Server Error", JOptionPane.WARNING_MESSAGE); | |
} | |
} | |
} catch (IOException ex) { | |
JOptionPane.showMessageDialog(null, ex.getMessage(), "Application Error", JOptionPane.WARNING_MESSAGE); | |
// Dialogs.showWarningDialog(stage, ex.getMessage(), "Application Error", "Error", Dialogs.DialogOptions.OK); | |
} catch (NoSuchAlgorithmException ex) { | |
JOptionPane.showMessageDialog(null, ex.getMessage(), "Server Error", JOptionPane.WARNING_MESSAGE); | |
} | |
} | |
void setStage(Stage stage) { | |
this.stage = stage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment