0

I am writing a Java Program which could be able to send SMS using Kannel. I have Configured Kannel in my VM Vare Virtual Machine (Red Hat). Kannel is working fine and I can send SMS by typing the url

http://192.168.214.128:13013/cgi-bin/sendsms?
       username=tester&password=foobar&to=03478847037&text=Mahtab

in my Windows browser. But when I access the same URL through Java Program I am getting this exception

java.io.IOException: Server returned HTTP response code: 400` for URL:
                     http://192.168.214.128:13013/cgi-bin/sendsms? 
                     username=tester&password=foobar&to=03478847037&text=Mahtab

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)

But when I paste the same url string in browser I am able to send the SMS.

code is attached

URL url = new URL("http://192.168.214.128:13013/cgi-bin/sendsms?username=tester&password=foobar&to=03478847037&text=Mahtab");

System.out.println(param.toString());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {answer.append(line);}
writer.close();
reader.close();

System.out.println(answer.toString());

Now please help me in this regard what I am missing???

Thor
  • 6,607
  • 13
  • 62
  • 96
  • Perhps the user-agent is missing/invalid. You should inspect this with _Wireshark_ and compare the results. – Thor Jun 03 '12 at 07:42
  • If I redirect the response in a jsp page to the same URL. It is successful but it only gives me an error when I try to access it through the above code ........... Actually redirecting the response does not solve my issue because I need to send Bulk SMSs to many number thats why I am Requesting the mentioned URL. – Mahtab Rasheed Jun 03 '12 at 07:52
  • I think there is no need to specify the user-agent .... as this code is successfully used by others ...... you can check it [here](http://stackoverflow.com/questions/10194047/sending-arabic-sms-in-kannel) – Mahtab Rasheed Jun 03 '12 at 07:53
  • ok, but you should check what is really transmitted on the wire! – Thor Jun 03 '12 at 08:06

1 Answers1

1

I have solved this problem ............ actually code and every thing was right. The only problem was Netbeans. I had not cleaned the project when made some changes ...... that's why it was not giving the desired outcome ..... I cleaned the project and then build it... and I was successful so lesson learnt is that some time you are logically true but unfortunately your IDE is doing a little error which teases you the most.. @thor thanks for helping