[英]Can't get inputstream from opend url?
I am trying to send sms from my web application using sms gateway. 我正在尝试使用短信网关从我的Web应用程序发送短信。 In the below code
con.getInputStream();
在下面的代码中
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();
在控制台中,它
url ouotput2
在con.getInputStream();
之后打印到url ouotput2
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. 我在url中的'apikey','sender'和'to'参数中提到了“xxxxxxxxxxxxxxx”。但在我的程序中,我使用的是网关提供商提供的内容。
Why do you get the OutputStream at all? 你为什么要得到OutputStream呢? And then not send something?
然后不发送什么? That's working for request-method POST only.
这仅适用于请求方法POST。
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: 您的API网址只会被视为“您的”字样 ,如下所示:
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 由于消息字之间的空间,因此它响应403错误
So, You have to encode the url using URLEncoder : 所以,你必须使用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.