I am trying to send sms from my web application using sms gateway. In the below code con.getInputStream();
is not working when ever the control comes there program throws exception.
public String process_sms(String mob_no,String message) throws IOException, KeyManagementException, NoSuchAlgorithmException
{
message=URLEncoder.encode(message, "UTF-8");
URL url = new URL("https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=Your One Time Password is {$No} ");
System.out.println("url look like " + url );
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("url opend" );
con.setRequestMethod("GET");
System.out.println("url method" );
con.setDoOutput(true);
System.out.println("url output" );
con.getOutputStream();
System.out.println("url ouotput2" );
con.getInputStream();
System.out.println("url input" );
BufferedReader rd;
String line;
String result = "";
rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
System.out.println("url input reader" );
while ((line = rd.readLine()) != null)
{
System.out.println("url input line" );
result += line;
}
rd.close();
System.out.println("Result is" + result);
return result;
}
In console it prints till url ouotput2
after that con.getInputStream();
not working. I don't know what is an issue.Can any one help me to fix this.
error:
type Exception report
message Server returned HTTP response code: 403 for URL: https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxx&to=xxxxxxxxx&message=Your One Time Password is {$No}
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.IOException: Server returned HTTP response code: 403 for URL: https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxxx&message=Your One Time Password is {$No}
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1628)
sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
send_sms.process_sms(send_sms.java:92)
send_sms.doPost(send_sms.java:58)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.62 logs.
I mentioned "xxxxxxxxxxxxxxx" in 'apikey','sender' and 'to' parameter in url.but in my program i use what ever it was given by the gateway provider.
Why do you get the OutputStream at all? And then not send something? That's working for request-method POST only.
Just as well, you open input stream twice - where's that second stream supposed to come from?
Try it this way:
public String process_sms(String mob_no,String message) throws IOException, KeyManagementException, NoSuchAlgorithmException
{
message=URLEncoder.encode(message, "UTF-8");
URL url = new URL("https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=Your One Time Password is {$No} ");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("url opend" );
String line;
String result = "";
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
System.out.println("url input reader: " + rd);
while ((line = rd.readLine()) != null)
{
System.out.println("url input line" );
result += line;
}
rd.close();
System.out.println("Result is" + result);
return result;
}
Your API URL is considered only till word "your" as below:
https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=Your
Because of space between message words so it is responding with 403 Error
So, You have to encode the url using URLEncoder :
String message = URLEncoder.encode("Your One Time Password is {$No}", "UTF-8");
URL url = new URL("https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=" + message);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.