我正在尝试使用短信网关从我的网络应用程序发送短信。在下面的代码con.getInputStream();
无法控制到来时程序抛出异常。
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;
}
在控制台中,在url ouotput2
无效后,它会打印到con.getInputStream();
。我不知道是什么问题。任何人都可以帮我解决这个问题。
错误:
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.
我在url中的'apikey','sender'和'to'参数中提到了“xxxxxxxxxxxxxxx”。但在我的程序中,我使用的是网关提供商提供的内容。
答案 0 :(得分:0)
为什么要获得OutputStream?然后不发送什么?那只适用于请求方法POST。
同样,您打开输入流两次 - 第二个流应该来自何处?
以这种方式尝试:
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;
}
答案 1 :(得分:0)
您的API网址只会被视为“您的”,如下所示:
https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=Your
由于消息字之间的空格,因此它响应403错误
所以,你必须使用URLEncoder编码url:
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);