2016-12-14 7 views
0
私はHttpURLConnectionの行動を理解しようとしています

上の要求を書いて、これまでのところ、私が知って持っているときにIOExceptionがすべてについても、など要求はサーバーに届いていますか?出力ストリーム

は、今私が成功したURLの接続をオープンしていると言うことができ、時間を読み出し、時間を接続しますデータを書き込むためにoutputStreamを得ました。

これで、サーバーにデータを書き込もうとしましたが、out.write(requestByte); したがって、もし私がIO例外を受け取るのであれば、out.write(requestByte)それはどういう意味ですか?

リクエストはサーバーに届いているかどうかを確認します。ここで

 try { 
       out.write(requestByte); 
       logger.info("API Request sent at : " + sdf.format(new Date())); 
       out.close(); 
      } catch (IOException e) { 
       logger.info("Exception while writing request to output stream"); 
       e.printStackTrace(); 
      } 

は完全なコードです....

try { 
      bridgeUrl = new URL(url); 

      if (connectionTime == null) { 
       connectionTime = StartupCache.getInstance().getConfigByKey(getUniqueKey(ConfigurationConstant.CONNECTION_TIME_OUT)); 
       //connectionTime = TransactionPersistence.getInstance().getConfigByConfigKey(ConfigurationConstant.CONNECTION_TIME_OUT); 
       if (connectionTime != null && connectionTime.getConfigValue().trim().length() > 0) { 
        connectionTimeOutInMilliSec = Integer.valueOf(connectionTime.getConfigValue()); 
       } 
      } 

      if (bridgeUrl.getProtocol().equalsIgnoreCase("https")) { 
       ignoreSslCertificateAuthentication(); 
       httpConn = (HttpsURLConnection) bridgeUrl.openConnection(); 
      } else { 
       httpConn = (HttpURLConnection) bridgeUrl.openConnection(); 
      } 
      logger.info("\ Request : "+request+"\n"); 

      request="JsonData="+request; 
      byte[] requestByte = request.getBytes(); 
      httpConn.setRequestProperty("Content-Length", String.valueOf(requestByte.length)); 
      httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      httpConn.setRequestMethod("POST"); 
      httpConn.setDoOutput(true); 
      httpConn.setDoInput(true); 
      httpConn.setConnectTimeout(connectionTimeOutInMilliSec); 
      httpConn.setReadTimeout(connectionTimeOutInMilliSec); 
      out = httpConn.getOutputStream(); 
      try { 
       out.write(requestByte); 
       logger.info("API Request sent at : " + sdf.format(new Date())); 
       out.close(); 
      } catch (IOException e) { 
       logger.info("Exception while writing request to output stream"); 
       e.printStackTrace(); 
      } 
     }catch(MalformedURLException exMurl){ 
      logger.info(exMurl.getMessage()); 
      throw new ApiRequestPostingException("Exception occured while posting request to server"); 
     } 
     catch(SocketTimeoutException stout){ 
      logger.error(stout.getMessage()); 
      stout.printStackTrace(); 

      if (httpConn != null) { 
       logger.info("Closing httpConn in exception blcok of output stream connection...."); 
       httpConn.disconnect(); 
      } 

      throw new ApiRequestPostingException("Exception occured while posting request to server"); 
     }catch(IOException ioex){ 
      logger.error(ioex.getMessage()); 
      ioex.printStackTrace(); 

      if (httpConn != null) { 
       logger.info("Closing httpConn in exception blcok of output stream connection...."); 
       httpConn.disconnect(); 
      } 

      throw new ApiRequestPostingException("Exception occured while posting request to server"); 
     } 

答えて

0

私はそれが意味、その後どのようなラインout.write(requestByte)でのIOExceptionを取得しますか?

リクエストはサーバーに届いているかどうかを確認します。

それはあなたがキャッチしたIOExceptionのビットによって異なりますが、そのほとんどは接続が致命的に壊れていることを示しています。データが送信されたか、少なくともローカルソケットの送信バッファにバッファされていれば、IOExceptionはありませんでした。

入力する必要がありますのでご注意ください。最低でもgetResponseCode()に電話し、結果を記録する必要があります。

+0

IOExceptionを取得していないということは、データがサーバーに到達したことを意味しないことに言及する価値があります。 –

+0

@ EJPご清聴ありがとうございます。 – ROHIT

関連する問題