vendredi 29 mai 2015

curl request in java umwandeln

Hallo Zusammen

Ich habe folgende cURL request:

curl -ikX POST -d "{\"outboundSMSMessageRequest\":{\"senderAddress\":\"tel:+40000000000\", \"address\":[\"tel:+4179xxxxxxx\"],\"outboundSMSTextMessage\":{\"message\":\"The Message\"},\"clientCorrelator\":\"any id\"}}" -H "Content-Type:application/json" -H "Accept:application/json" -H "client_id: %YOUR_CLIENT_ID%" http://ift.tt/1HA0oru

Meine Aufgabe ist es, dass in Java zu realisierten, also eine Java Klasse zu schreiben.

Ich habe folgende Java-Klasse geschrieben:

Java Code:

  1.  
  2. package sms;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.ProtocolException;
  11. import java.net.URL;
  12. import java.net.URLEncoder;
  13.  
  14. import com.google.gson.Gson;
  15.  
  16.  
  17. /**
  18.  *
  19.  * @author Behar
  20.  * Sms senden
  21.  */
  22. public class Sms {
  23.  
  24. private String to; //Telefonnummer
  25. private String text; //Nachricht
  26. private int tokenLength; //??
  27. private String expireTime; //?
  28.  
  29.  
  30.  
  31. /**
  32.   * @param apiKey
  33.   * @param sms
  34.   * @throws IOException
  35.   */
  36. public static void senden (String apiKey, Sms sms) throws IOException {
  37. try{
  38. //Json Parameter erstellen
  39. Gson gson = new Gson();
  40. String json = gson.toJson(sms);
  41.  
  42. //URL
  43. URL url = new URL("http://ift.tt/1HR1cxL;);
  44.  
  45. //Verbindung offnen
  46. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  47.  
  48. //Methode
  49. con.setRequestMethod("POST");
  50. //Header setezn
  51. con.setRequestProperty("client_id", apiKey);
  52. con.setRequestProperty("Accept", "application/json; charset=utf-8");
  53. con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  54.  
  55. con.setDoOutput(true);
  56.  
  57. //Json data im Body hinzufügen
  58. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  59. String param = "json=" + URLEncoder.encode(json, "UTF-8");
  60. wr.write(param.getBytes());
  61.  
  62. System.out.println(json);
  63. wr.flush();
  64. wr.close();
  65.  
  66. // Get the response
  67. int responseCode = con.getResponseCode();
  68. System.out.println("\nSending 'POST' request to URL : " + url);
  69. System.out.println("Response Code : " + responseCode);
  70.  
  71. new InputStreamReader(con.getInputStream()));
  72. String inputLine;
  73. StringBuffer response = new StringBuffer();
  74.  
  75. while ((inputLine = in.readLine()) != null) {
  76. response.append(inputLine);
  77. }
  78. in.close();
  79.  
  80. // 7. Print result
  81. System.out.println(response.toString());
  82.  
  83. }
  84. e.printStackTrace();
  85. }
  86.  
  87. }
  88.  
  89. //Getter und Setter Methoden
  90.  
  91. /**
  92.   *
  93.   * @return Nachricht
  94.   */
  95. public String getTo() {
  96. return to;
  97. }
  98.  
  99. /**
  100.   *
  101.   * @param to Telefonnummer des Senders
  102.   */
  103. public void setTo(String to) {
  104. this.to = to;
  105. }
  106.  
  107. /**
  108.   * @return Nachricht
  109.   */
  110. public String getText() {
  111. return text;
  112. }
  113.  
  114. public void setText(String text) {
  115. this.text = text;
  116. }
  117.  
  118. public int getTokenLength() {
  119. return tokenLength;
  120. }
  121.  
  122. public void setTokenLength(int tokenLength) {
  123. this.tokenLength = tokenLength;
  124. }
  125.  
  126. public String getExpireTime() {
  127. return expireTime;
  128. }
  129.  
  130. public void setExpireTime(String expireTime) {
  131. this.expireTime = expireTime;
  132. }
  133.  
  134. }


Leider funktioniert die Klasse nicht ganz.
Ich bekomme Response Code : 400

Ich glaube es stimmt mit den Parametern etwas nicht.

Klasse senden:

Java Code:

  1.  
  2. <font color="#931A68">package<font color="#000000"> sms;</font></font>
  3.  
  4.  
  5. <font color="#931a68">import</font> java.io.IOException;
  6.  
  7.  
  8. <font color="#931A68">publicclass<font color="#000000"><u>Send</u></font><font color="#000000"> {</font></font>
  9.  
  10. <font color="#931a68">public</font> <font color="#931a68">static</font> <font color="#931a68">void</font> <u>main (String [] arg) </u><font color="#931a68"><u>throws</u></font><u> IOException</u> {
  11.  
  12. Sms sms = <font color="#931a68">new</font> Sms();
  13. sms.setText(<font color="#3933ff">"Hallo Test"</font>);
  14. sms.setTo(<font color="#3933ff">"+41791749697"</font>);
  15. sms.setTokenLength(8);
  16. sms.setExpireTime(<font color="#3933ff">"60"</font>);
  17.  
  18. Sms.senden(<font color="#3933ff">"?? :)"</font>, sms);
  19. }
  20.  
  21.  
  22. }


Ich wäre euch sehr dankbar wenn ihr mir weiterhelfen könntet

Danke im Voraus!


curl request in java umwandeln

0 commentaires:

Enregistrer un commentaire