[英]Can't get inputstream from opend url?
我正在尝试使用短信网关从我的Web应用程序发送短信。 在下面的代码中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();
之后打印到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”。但在我的程序中,我使用的是网关提供商提供的内容。
你为什么要得到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;
}
您的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.