2016-04-29 69 views
1

我想從我的web應用程序使用短信網關發送短信。在以下代碼中,con.getInputStream();在有控制到達時不起作用,程序拋出異常。無法從opend url獲取inputstream?

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 ouotput2con.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.but在我的計劃中提到的「apikey」「XXXXXXXXXXXXXXX」,「發件人」和「到」的參數我使用過它是由網關給出供應商。

+2

HTTP返回碼403表示「禁止」。我想你的網址或你的api鍵是錯誤的。 –

+0

@ThomasStets不,我只是複製並通過瀏覽器選項卡中的網址,它工作正常。 – KVK

+0

@KVK 403意味着禁止。您在使用apikey或網址時出現問題。可能的檢查標題以及某些cookie。 – user2494817

回答

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;    
} 
+0

我累了,但它顯示我無效模板error.because我使用短信網關的帳戶帳戶。 – KVK

+0

那麼現在你確實從SMS網關獲取了一些東西?所以與* InputStream *問題似乎解決了? – Jan

0

您的API URL被認爲只能做到單詞「你」如下:

https://instantalerts.co/api/web/send/?apikey=6d6ra0u305nggr0cvrxxxxxxxxxxxxxx&sender=xxxxxx&to=xxxxxxxxxx&message=Your 

正因如此它與響應消息字之間的空間403錯誤

所以,你必須使用URLEncoder對網址進行編碼:

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);