gpt4 book ai didi

java - 无法从打开的网址获取输入流?

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:50 26 4
gpt4 key购买 nike

我正在尝试使用短信网关从我的网络应用程序发送短信。在下面的代码中,当控件出现时,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”。但在我的程序中,我使用网关提供商提供的内容。

最佳答案

你为什么要得到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;
}

关于java - 无法从打开的网址获取输入流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36930966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com