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

标签 java httpurlconnection

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

相关文章:

java - [未解决]列表模型 list.removeAllElements java swing 的问题

java - 如何在 Spring Boot 中从 application.properties 分配注释值?

java - Gluon Project-当我运行/.gradle运行时找不到Springboot依赖关系

android - 如何在android中使用presignedurl从aws S3服务器上传图像

java - Apache poi 在图像中放置超链接

java - jackson 序列化不包括 double 值 0.0

java - 加载网页时的点击次数?

Java - 连接到亚马逊

java - 获取 java.net.SocketTimeoutException : Connection timed out in android

java - 如何重用 HttpUrlConnection?