2014-09-24 5 views
5

ich SMS zu senden bin versucht zu senden:„Server zurückgegeben HTTP-Antwortcode: 400“ mit dem folgenden Code bei dem Versuch, eine SMS in Java

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.Iterator; 
import java.util.Vector; 

public class SMS { 

    public static void send(String uid, String pwd, String phone, String msg) 
      throws Exception { 

     if ((uid == null) || (uid.length() == 0)) { 
      throw new IllegalArgumentException("User ID should be present."); 
     } 

     uid = URLEncoder.encode(uid, "UTF-8"); 

     if ((pwd == null) || (pwd.length() == 0)) { 
      throw new IllegalArgumentException("Password should be present."); 
     } 

    pwd = URLEncoder.encode(pwd, "UTF-8"); 

     if ((phone == null) || (phone.length() == 0)) { 
      throw new IllegalArgumentException(
        "At least one phone number should be present."); 
     } 

    if ((msg == null) || (msg.length() == 0)) { 
      throw new IllegalArgumentException("SMS message should be present."); 
     } 

     msg = URLEncoder.encode(msg, "UTF-8"); 

     Vector<Long> numbers = new Vector<Long>(); 

     if (phone.indexOf(59) >= 0) { 
      String[] pharr = phone.split(";"); 
      for (String t : pharr) 
       try { 
        numbers.add(Long.valueOf(t)); 
       } catch (NumberFormatException ex) { 
        throw new IllegalArgumentException(
          "Give proper phone numbers."); 
       } 
     } else { 
      try { 
       numbers.add(Long.valueOf(phone)); 
      } catch (NumberFormatException ex) { 
       throw new IllegalArgumentException("Give proper phone numbers."); 
      } 
     } 

     if (numbers.size() == 0) { 
      throw new IllegalArgumentException(
        "At least one proper phone number should be present to send SMS."); 
     } 
     String temp = ""; 
     String content = "username=" + uid + "&password=" + pwd; 
     URL u = new URL("http://www.way2sms.com/auth.cl"); 
     HttpURLConnection uc = (HttpURLConnection) u.openConnection(); 
     uc.setDoOutput(true); 
     uc.setRequestProperty(
       "User-Agent", 
       "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5"); 
     uc.setRequestProperty("Content-Length", 
       String.valueOf(content.length())); 
     uc.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     uc.setRequestProperty("Accept", "*/*"); 
     uc.setRequestProperty("Referer", "http://www.way2sms.com//entry.jsp"); 
     uc.setRequestMethod("POST"); 
     uc.setInstanceFollowRedirects(false); 

     PrintWriter pw = new PrintWriter(new OutputStreamWriter(
       uc.getOutputStream()), true); 
     pw.print(content); 
     pw.flush(); 
     pw.close(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(
       uc.getInputStream())); 

     while ((temp = br.readLine()) != null) { 
      System.out.println(temp); 
     } 

     String cookie = uc.getHeaderField("Set-Cookie"); 

     u = null; 
     uc = null; 
     for (Iterator<Long> localIterator = numbers.iterator(); localIterator 
       .hasNext();) { 
      long num = ((Long) localIterator.next()).longValue(); 

      content = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo=" 
        + num + "&textArea=" + msg; 
      u = new URL("http://www.way2sms.com/FirstServletsms?custid="); 
      uc = (HttpURLConnection) u.openConnection(); 
      uc.setDoOutput(true); 
      uc.setRequestProperty(
        "User-Agent", 
        "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5"); 
      uc.setRequestProperty("Content-Length", 
        String.valueOf(content.getBytes().length)); 
      uc.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded"); 
      uc.setRequestProperty("Accept", "*/*"); 
      uc.setRequestProperty("Cookie", cookie); 
      uc.setRequestMethod("POST"); 
      uc.setInstanceFollowRedirects(false); 
      pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()), 
        true); 
      pw.print(content); 
      pw.flush(); 
      pw.close(); 
      br = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
      while ((temp = br.readLine()) != null) 
       ; 
      br.close(); 
      u = null; 
      uc = null; 
     } 

     u = new URL("http://wwwa.way2sms.com/jsp/logout.jsp"); 
     uc = (HttpURLConnection) u.openConnection(); 
     uc.setRequestProperty(
       "User-Agent", 
       "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5"); 
     uc.setRequestProperty("Accept", "*/*"); 
     uc.setRequestProperty("Cookie", cookie); 
     uc.setRequestMethod("GET"); 
     uc.setInstanceFollowRedirects(false); 
     br = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
     while ((temp = br.readLine()) != null) 
      ; 
     br.close(); 
     u = null; 
     uc = null; 
    } 

    public static void main(String args[]) throws Exception { 
     //SMS s = new SMS(); 
     SMS.send("9999999999", "password", "8888888888", "Hi How Are u !!"); 

    } 

} 

Aber, wenn ich diesen Code ausführen, ich bin die immer folgende Fehlermeldung:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://www.way2sms.com/FirstServletsms?custid=XXXXXXXXX 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615) 
    at SMS.send(SMS.java:115) 
    at SMS.main(SMS.java:142) 
+0

http://stackoverflow.com/q/9349703. Es scheint, dass die wahrscheinlichste Ursache darin besteht, dass Sie eine ungültige URL senden. Überprüfen Sie die URL, die Sie senden, und stellen Sie sicher, dass sie korrekt ist. –

+0

@Robert Hallo, Danke. Ich habe die URL überprüft, es ist korrekt. Trotzdem kann ich keine SMS versenden. –

+0

'http: //www.way2sms.com/FirstServletsms? Custid =' sieht für mich nicht wie eine gültige URL aus. Außerdem scheint es, dass Sie vergessen haben, 'content' in demselben Code zu verwenden ... –

Antwort

0

Sie sind nicht der erste, um das Problem zu haben, aber es scheint zu sein, dass die URL, die Sie senden, oder die Daten, die Sie senden möchten, ist falsch.

Einige Vorschläge gefunden here:

  • Verwendung Apache Commons HTTP-Client
  • Prüfen Sie, ob dies nicht aufgrund eines Proxy auf Ihrer Seite sein könnte (versuchen, von woanders)
  • Versuchen Sie stattdessen mit GET von POST
Verwandte Themen