001 /* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016 package org.opengion.fukurou.util; 017 018 import java.io.BufferedInputStream; 019 import java.io.BufferedReader; 020 import java.io.File; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.InputStreamReader; 024 import java.io.OutputStream; 025 import java.io.PrintStream; 026 import java.io.PrintWriter; 027 import java.io.UnsupportedEncodingException; 028 import java.net.HttpURLConnection; 029 import java.net.InetSocketAddress; 030 import java.net.Proxy; 031 import java.net.SocketAddress; 032 import java.net.URL; 033 import java.net.URLConnection; 034 035 import org.apache.commons.codec.binary.Base64; 036 037 /** 038 * URLConnect ã¯ã€æŒ‡å®šã?URL ã«ã‚¢ã‚¯ã‚»ã‚¹ã—ã¦ã€æƒ…å ±/ãƒ??ã‚¿ã‚’å–å¾—ã—ã¾ã™ã? 039 * URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã«ã‚ˆã‚Šã€ã‚¨ãƒ³ã‚¸ãƒ³ã§ã¯å?¨®å‡¦ç?‚’実行ã•ã›ã‚‹ã“ã¨ãŒå¯èƒ½ã«ãªã‚Šã¾ã™ã? 040 * 例ãˆã°ã€å¸³ç¥¨ãƒ??モンã®èµ·å‹•ã‚„ã€?•·æ™‚é–“ã‹ã‹ã‚‹å?ç??実行ãªã©ã§ã™ã? 041 * ãªãŠã?URLã«å¼•æ•°ãŒä»˜ãå ´åˆã?ã€ãƒ€ãƒ–ルコーãƒ??ã‚·ãƒ§ãƒ³ã§æ‹¬ã£ã¦ä¸‹ã•ã?? 042 * - 付ã引数ã¯ã€æŒ‡å®šé?番ã¯ã€?–¢ä¿‚ã‚りã¾ã›ã‚“ã€? ç„¡ã—引数(url,user:passwd)ã¯ã€? 043 * é ?•ªãŒã‚りã¾ã™ã? 044 * 045 * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd] 046 * 047 * args[*] : [-info/-data] æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)ã€? 048 * args[*] : [-post=ファイルå] POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)ã€? 049 * args[*] : [-encode=UTF-8] エンコードをæŒ?®šã—ã¾ã?é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)ã€? 050 * args[*] : [-out=ファイルå] çµæžœã‚’指定ã•れãŸãƒ•ァイルエンコードã§ãƒ•ァイルã«å‡ºåŠ›ã—ã¾ã™ã? 051 * args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)ã€? 052 * args[A] : url ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã™ã? 053 * args[B] : [user:passwd] BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã™ã? 054 * 055 * ※ プãƒã‚ã‚·è¨å®šã??“ã¤ã®æ–¹æ³? 056 * プãƒã‚ã‚·è¨å®šã«ã¯ã€?¼“ã¤ã®æ–¹æ³•ãŒã‚りã¾ã™ã? 057 * 1. 058 * URL url = URL( "http",proxyHost,proxyPort, url ); 059 * URLConnection urlConn = url.openConnection(); 060 * 2. 061 * SocketAddress scaddr = new InetSocketAddress( proxyHost, proxyPort ); 062 * Proxy proxy = new Proxy( Proxy.Type.HTTP, scaddr ); 063 * URL url = new Url( url ); 064 * URLConnection urlConn = url.openConnection( proxy ); 065 * 3. 066 * System.setProperty( "http.proxyHost",host ); 067 * System.setProperty( "http.proxyPort",String.valueOf( port ) ); 068 * URL url = new Url( url ); 069 * URLConnection urlConn = url.openConnection(); 070 * System.clearProperty( "http.proxyHost" ); 071 * System.clearProperty( "http.proxyPort" ); 072 * 073 * 1. ã€?. ã®æ–¹æ³•ã?ã€urlConn.getContentType() を実行ã™ã‚‹ã¨ã€ã‚¨ãƒ©ãƒ¼ã«ãªã‚Šã¾ã™ã?(åŽŸå› ä¸æ?) 074 * 3. ã®æ–¹æ³•ã§ã¯ã€ã?ルãƒã‚¹ãƒ¬ãƒ?ƒ‰ã§å®Ÿè¡Œã™ã‚‹å?åˆã«ã€å•題ãŒç™ºç”Ÿã—ã¾ã™ã? 075 * 本クラスã§ã¯ã€æ–¹æ³•ï¼?を使用ã—ã¦ã?¾ã™ã? 076 * 077 * @version 4.0 078 * @author Kazuhiko Hasegawa 079 * @since JDK5.0, 080 */ 081 public class URLConnect { 082 private static final String CR = System.getProperty("line.separator"); 083 084 // private static final String ENCODE = "UTF-8"; 085 086 private final String urlStr ; 087 private final String userPass ; 088 089 private int rpsCode = -1; 090 private String rpsMethod = null; 091 private String rpsMessage = null; 092 private String type = null; 093 private String charset = null; 094 private String postData = null; 095 private int timeout = -1; // 5.8.8.1 (2015/06/12) timeout属æ?追åŠ? 096 private long length = -1; 097 private long date = -1; 098 private long modified = -1; 099 private boolean isPost = false; 100 private URLConnection conn = null; 101 private Proxy proxy = Proxy.NO_PROXY; 102 103 // 5.8.3.0 (2015/01/09) 追åŠ? 104 private String[] propKeys; 105 private String[] propVals; 106 107 /** 108 * コンストラクター 109 * 110 * @param url 接続ã™ã‚‹ã‚¢ãƒ‰ãƒ¬ã‚¹ã‚’指定ã—ã¾ã™ã?(http://server:port/dir/file.html) 111 * @param pass ユーザー?šãƒ‘スワーãƒ?èªè¨¼æŽ¥ç¶šãŒå¿?¦ãªå ´å? 112 */ 113 public URLConnect( final String url, final String pass ) { 114 urlStr = url; 115 userPass = pass; 116 } 117 118 /** 119 * æŒ?®šã?URLã«å¯¾ã—ã¦ã€ã‚³ãƒã‚¯ãƒˆã™ã‚‹ã?ã«ä½¿ç”¨ã™ã‚‹ãƒ—ãƒã‚ã‚·è¨å®šã‚’行ã„ã¾ã™ã? 120 * ã“ã?ã¨ãã«ã€ã?ãƒ?ƒ€ãƒ¼æƒ??ã‚’å?部変数ã«è¨å®šã—ã¦ãŠãã¾ã™ã? 121 * 122 * @param host 接続ã™ã‚‹ã?ãƒã‚ã‚·ã®ãƒ›ã‚¹ãƒˆå 123 * @param port 接続ã™ã‚‹ã?ãƒã‚ã‚·ã®ãƒã?ãƒˆç•ªå· 124 */ 125 public void setProxy( final String host,final int port ) { 126 // æ–¹æ³?. 127 SocketAddress scaddr = new InetSocketAddress( host, port ); 128 proxy = new Proxy( Proxy.Type.HTTP, scaddr ); 129 } 130 131 /** 132 * æŒ?®šã?URLã«å¯¾ã—ã¦ã€ã‚³ãƒã‚¯ãƒˆã—ã¾ã™ã? 133 * ã“ã?ã¨ãã«ã€ã?ãƒ?ƒ€ãƒ¼æƒ??ã‚’å?部変数ã«è¨å®šã—ã¦ãŠãã¾ã™ã? 134 * 135 * @og.rev 4.0.1.0 (2007/12/12) Postã§è¤?•°ã‚ーを使ãˆã‚‹ã‚ˆã†ã«ä¿®æ£ 136 * @og.rev 5.1.6.0 (2010/05/01) charsetを指定ã§ãるよã†ã«ã™ã‚‹ 137 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 138 */ 139 public void connect() throws IOException { 140 conn = getConnection(); 141 142 if( isPost ) { 143 conn.setDoOutput( true ); // POSTå¯èƒ½ã«ã™ã‚‹ 144 145 OutputStream os = null; // POST用ã®OutputStream 146 PrintStream ps = null; 147 try { 148 os = conn.getOutputStream(); // POST用ã®OutputStreamã‚’å–å¾? 149 // 5.1.6.0 (2010/05/01) 150 if( charset != null ) { 151 ps = new PrintStream( os, false, charset ); 152 } 153 else { 154 ps = new PrintStream( os ); 155 } 156 ps.print( postData ); // 4.1.0.0 (2007/12/22) 157 } 158 finally { 159 Closer.ioClose( ps ); // close 処ç?™‚ã® IOException ã‚’ç„¡è¦? 160 Closer.ioClose( os ); // close 処ç?™‚ã® IOException ã‚’ç„¡è¦? 161 } 162 } 163 else { 164 // GET 時ã?コãƒã‚¯ã‚·ãƒ§ãƒ³æŽ¥ç¶? 165 conn.connect(); 166 } 167 168 setInfo( conn ); 169 } 170 171 /** 172 * U接続å?ã®ãƒ??ã‚¿ã‚’å–å¾—ã—ã¾ã™ã? 173 * 174 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã? 175 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã? 176 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€? 177 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã? 178 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã® 179 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€? 180 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€? 181 * 本æ¥ã¯ã€Stream ã®ã¾ã¾å‡¦ç?™ã‚‹ã“ã¨ã§ã€ãƒã‚¤ãƒŠãƒªãƒ??タも扱ãˆã¾ã™ãŒã€ã“ã“ã§ã¯ã€? 182 * ãƒ?‚ストデータ(String)ã«å¤‰æ›ã—ã¦ä½¿ç”¨ã§ãã‚‹ãƒ??ã‚¿ã®ã¿æ‰±ãˆã¾ã™ã? 183 * 184 * @return æŽ¥ç¶šçµæžœ 185 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 186 */ 187 public String readData() throws IOException { 188 if( conn == null ) { 189 String errMsg = "connect() ã•れã¦ã?¾ã›ã‚“。データå–å¾—å‰ã«connect()ã—ã¦ãã ã•ã„ã€?; 190 throw new RuntimeException( errMsg ); 191 } 192 193 BufferedReader reader = null; 194 StringBuilder buf = new StringBuilder(); 195 try { 196 reader = getReader(); 197 198 String line ; 199 while( (line = reader.readLine()) != null ) { 200 buf.append( line ).append( CR ); 201 } 202 } 203 catch( UnsupportedEncodingException ex ) { 204 String errMsg = "æŒ?®šã•ã‚ŒãŸæ–?—エンコーãƒ?‚£ãƒ³ã‚°ãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? + CR 205 + " url=[" + urlStr + "]" 206 + " charset=[" + charset + "]" ; 207 throw new RuntimeException( errMsg,ex ); 208 } 209 finally { 210 Closer.ioClose( reader ); 211 disconnect(); 212 } 213 214 return buf.toString(); 215 } 216 217 /** 218 * サーãƒã¸ã®ã»ã‹ã?è¦æ±‚ãŒä»Šå¾Œç™ºç”Ÿã—ãã†ã«ãªã?“ã¨ã‚’示ã—ã¾ã™ã? 219 * 220 */ 221 public void disconnect() { 222 if( conn instanceof HttpURLConnection ) { 223 ((HttpURLConnection)conn).disconnect() ; 224 } 225 } 226 227 /** 228 * URL 㨠ユーザー?šãƒ‘スワードを与ãˆã¦ã€URLConnectionã‚’è¿”ã—ã¾ã™ã? 229 * 230 * ユーザー?šãƒ‘スワーãƒ?ã?null ã§ãªã??åˆã?ã€BASCIèªè¨¼ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ç‚ºã€? 231 * BASE64Encoder を行ã£ã¦ã€Authorization プãƒãƒ‘ティーをè¨å®šã—ã¾ã™ã? 232 * ã“ã“ã§è¿”ã™ URLConnection ã¯ã€ã™ã§ã«ã€connect() メソãƒ?ƒ‰å®Ÿè¡Œæ¸ˆã¿ã® 233 * リモート接続ãŒå®Œäº?—ãŸçŠ¶æ…‹ã?オブジェクトã§ã™ã? 234 * 235 * @og.rev 5.8.3.0 (2015/01/09) ヘッãƒ?‰æŒ‡å®šã?ãŸã‚ã«setRequestPropertyã®å€¤ã‚’指定ã§ãるよã†ã«ã—ã¾ã™ã? 236 * @og.rev 5.8.8.1 (2015/06/12) 237 * 238 * @return URLConnectionオブジェクãƒ? 239 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 240 */ 241 // 5.1.5.0 (2010/04/01) SOAP対応ã«ã‚ˆã‚Šã€PROTECTEDåŒ? 242 protected URLConnection getConnection() throws IOException { 243 // private URLConnection getConnection() throws IOException { 244 final URL url = new URL( urlStr ); 245 246 // æ–¹æ³?. 247 URLConnection urlConn = url.openConnection( proxy ); 248 249 if( userPass != null ) { 250 // byte[] encoded = Base64.encodeBase64( userPass.getBytes() ); 251 // String userPassEnc = new String( encoded ); 252 byte[] encoded = Base64.encodeBase64( userPass.getBytes( StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対å¿? 253 String userPassEnc = new String( encoded,StringUtil.DEFAULT_CHARSET ); // 5.5.2.6 (2012/05/25) findbugs対å¿? 254 urlConn.setRequestProperty( "Authorization","Basic " + userPassEnc ); 255 } 256 257 // 5.8.3.0 (2015/01/09) RequestPropertyã®ã‚»ãƒ?ƒˆ 258 if( propKeys != null && propKeys.length > 0 ){ 259 for(int i = 0; i < propKeys.length; i++){ 260 urlConn.setRequestProperty( propKeys[i], propVals[i] ); 261 } 262 } 263 264 // 5.8.8.1 (2015/06/12) timeout属æ?追åŠ? 265 if( timeout >= 0 ) { 266 urlConn.setConnectTimeout( timeout * 1000 ); // 引数ã¯(ç§?ã€è¨å®šã?(ミリç§? 267 } 268 269 return urlConn ; 270 } 271 272 /** 273 * 接続å?ã®æƒ??ã‚’å?部変数ã«è¨å®šã—ã¾ã™ã? 274 * 275 * ã“ã“ã§ã¯ã€ã‚¿ã‚¤ãƒ?エンコーãƒ?レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?レスãƒãƒ³ã‚¹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸ ã‚’è¨å®šã—ã¾ã™ã? 276 * レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?レスãƒãƒ³ã‚¹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸ã¯ã€æŽ¥ç¶šã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ãŒã?HttpURLConnection ã® 277 * å ´åˆã?ã¿ã‚»ãƒ?ƒˆã•れã¾ã™ã? 278 * 途ä¸ã§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ãŸå?åˆã§ã‚‚ã?継続å?ç?§ãるよã†ã«ã—ã¾ã™ã?ã“れã¯ã€ã?ãƒã‚ã‚· 279 * è¨å®šã?方法ã«ã‚ˆã‚Šã€conn.getContentType() ã§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã™ã‚‹å?åˆãŒã‚ã‚‹ãŸã‚ã§ã™ã? 280 * 281 * @og.rev 5.5.9.1 (2012/12/07) charsetã¯ã€null ã®å ´åˆã?ã¿è¨å®šã—ã¾ã™ã? 282 * 283 * @param conn 接続å?ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ 284 */ 285 private void setInfo( final URLConnection conn ) { 286 try { 287 // 5.5.9.1 (2012/12/07) charsetã¯ã€null ã®å ´åˆã?ã¿è¨å®šã—ã¾ã™ã? 288 if( charset == null ) { charset = conn.getContentEncoding(); } 289 type = conn.getContentType() ; 290 length = conn.getContentLength(); 291 date = conn.getDate(); 292 modified= conn.getLastModified(); 293 294 if( charset == null && type != null ) { 295 int adrs = type.indexOf( "charset" ); 296 int adrs2 = type.indexOf( '=',adrs ); 297 if( adrs > 0 && adrs2 > adrs ) { 298 charset = type.substring( adrs2+1 ).trim(); 299 } 300 } 301 302 if( conn instanceof HttpURLConnection ) { 303 HttpURLConnection httpConn = (HttpURLConnection) conn; 304 rpsCode = httpConn.getResponseCode(); 305 rpsMethod = httpConn.getRequestMethod(); 306 rpsMessage = httpConn.getResponseMessage() + code2Message( rpsCode ); 307 } 308 } 309 // 4.0.0.0 (2007/11/29) Exception ã‹ã‚‰ã€IOException 㨠RuntimeException ã«å¤‰æ›´ 310 catch( IOException ex ) { 311 System.out.println( ex.getMessage() ); 312 } 313 catch( RuntimeException ex ) { 314 System.out.println( ex.getMessage() ); 315 } 316 } 317 318 /** 319 * URL æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 320 * 321 * @og.rev 4.3.4.4 (2009/01/01) メソãƒ?ƒ‰å変更 322 * 323 * @return URLæƒ?? 324 */ 325 public String getUrl() { return urlStr; } 326 327 /** 328 * setRequestPropertyã§ã‚»ãƒ?ƒˆã™ã‚‹ãƒ??ã‚¿ã‚’è¨å®šã—ã¾ã™ã? 329 * 330 * keys,valså??ã€ã‚«ãƒ³ãƒžåŒºåˆ?‚Šã§åˆ?§£ã—ã¾ã™ã? 331 * 332 * @og.rev 5.8.3.0 (2007/12/22) 追åŠ? 333 * @param keys パラメータã‚ー?ˆã‚«ãƒ³ãƒžåŒºåˆ?‚Š?? 334 * @param vals パラメータ?ˆã‚«ãƒ³ãƒžåŒºåˆ?‚Š?? 335 */ 336 public void setRequestProperty( final String keys, final String vals ) { 337 if( keys != null && keys.length() > 0 && vals != null && vals.length() > 0 ){ 338 propKeys = StringUtil.csv2Array( keys ); 339 propVals = StringUtil.csv2Array( vals ); 340 341 if( propKeys.length != propVals.length ) { 342 final String errMsg = "パラメータã®ã‚ーã¨ã€å?ã®æ•°ãŒä¸??ã—ã¾ã›ã‚“ã€? + CR 343 + " key=[" + keys + "]" + CR 344 + " val=[" + vals + "]" ; 345 throw new IllegalArgumentException( errMsg ); 346 } 347 } 348 } 349 350 /** 351 * POSTã™ã‚‹ãƒ??ã‚¿ã‚’è¨å®šã—ã¾ã™ã? 352 * 353 * POSTã™ã‚‹å ´åˆã?ã€connect() 処ç?‚’行ã†å‰ã«ã€ãƒ‡ãƒ¼ã‚¿ã‚’è¨å®šã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã? 354 * 355 * @og.rev 4.1.0.0 (2007/12/22) ã‚ーã¨å€¤ã®ã‚»ãƒ?ƒˆã‚’å–å¾—ã™ã‚‹ã‚ˆã?¤‰æ›´ 356 * @param data POSTãƒ??ã‚¿ 357 */ 358 public void setPostData( final String data ) { 359 postData = data; 360 if( postData != null && "?".indexOf( postData ) == 0 ) { // å…ˆé?ã®?を抜ã? 361 postData = postData.substring(1); 362 } 363 if( postData != null ) { isPost = true; } 364 } 365 366 /** 367 * タイãƒ?æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 368 * 369 * @return タイãƒ?æƒ?? 370 */ 371 public String getType() { return type; } 372 373 /** 374 * ãƒ??ã‚¿é‡?æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 375 * 376 * @return ãƒ??ã‚¿é‡?æƒ?? 377 */ 378 public long getLength() { return length; } 379 380 /** 381 * 作æ?æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 382 * 383 * @return 作æ?æ—¥æ™? 384 */ 385 public long getDate() { return date; } 386 387 /** 388 * æ›´æ–°æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 389 * 390 * @return æ›´æ–°æ—¥æ™? 391 */ 392 public long getModified() { return modified; } 393 394 /** 395 * çµæžœã‚³ãƒ¼ãƒ?æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã? 396 * 397 * @return çµæžœã‚³ãƒ¼ãƒ?æƒ?? 398 */ 399 public int getCode() { return rpsCode; } 400 401 /** 402 * メソãƒ?ƒ‰ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã? 403 * 404 * @return メソãƒ?ƒ‰ æƒ?? 405 */ 406 public String getMethod() { return rpsMethod; } 407 408 /** 409 * メãƒ?‚»ãƒ¼ã‚¸ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã? 410 * 411 * @return メãƒ?‚»ãƒ¼ã‚¸ æƒ?? 412 */ 413 public String getMessage() { return rpsMessage; } 414 415 /** 416 * ã‚ャラクタ æƒ??ã‚’å–å¾—ã—ã¾ã™ã? 417 * 418 * @return ã‚ャラクタ æƒ?? 419 */ 420 public String getCharset() { return charset; } 421 422 /** 423 * ã‚ャラクタ æƒ??ã‚’è¨å®šã—ã¾ã™ã? 424 * 425 * @param chset ã‚ャラクタ æƒ?? 426 */ 427 public void setCharset( final String chset ) { charset = chset; } 428 429 /** 430 * 接続タイãƒ?‚¢ã‚¦ãƒˆæ™‚é–“ã‚’(ç§?ã§æŒ?®šã—ã¾ã? 431 * 432 * 実際ã«ã¯ã€java.net.URLConnection#setConnectTimeout(int) ã« 1000å€ã—ã¦è¨å®šã•れã¾ã™ã? 433 * 0 ã¯ã€ç„¡é™ã?タイãƒ?‚¢ã‚¦ãƒˆã?マイナスã¯ã€è¨å®šã—ã¾ã›ã‚“ã€?ã¤ã¾ã‚ŠJavaã®åˆæœŸå€¤ã®ã¾ã¾) 434 * 435 * @og.rev 5.8.8.1 (2015/06/12) timeout属æ?追åŠ? 436 * 437 * @param tout タイãƒ?‚¢ã‚¦ãƒˆæ™‚é–?ç§? (ゼãƒã¯ã€ç„¡åˆ¶é™? 438 * @see java.net.URLConnection#setConnectTimeout(int) 439 */ 440 public void setTimeout( final int tout ) { 441 timeout = tout; 442 } 443 444 /** 445 * 接続å?ã®ãƒ??ã‚¿ã®ãƒªãƒ¼ãƒ??ã‚’å–å¾—ã—ã¾ã™ã? 446 * 447 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã? 448 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã? 449 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€? 450 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã? 451 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã® 452 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€? 453 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€? 454 * 455 * @return æŽ¥ç¶šçµæžœã®ãƒªãƒ¼ãƒ?? 456 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 457 */ 458 public BufferedReader getReader() throws IOException { 459 InputStream in = conn.getInputStream(); 460 461 final BufferedReader reader ; 462 if( charset != null ) { 463 reader = new BufferedReader( new InputStreamReader( in,charset ) ); 464 } 465 else { 466 // reader = new BufferedReader( new InputStreamReader( in ) ); 467 reader = new BufferedReader( new InputStreamReader( in,StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対å¿? 468 } 469 470 return reader; 471 } 472 473 /** 474 * 接続å?ã®ãƒ??ã‚¿ã®å…¥åŠ›ã‚¹ãƒˆãƒªãƒ¼ãƒ?‚’å–å¾—ã—ã¾ã™ã? 475 * 476 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã? 477 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã? 478 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€? 479 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã? 480 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã® 481 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€? 482 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€? 483 * 484 * @og.rev 5.4.2.0 (2011/12/01) æ–°è¦è¿½åŠ? 485 * 486 * @return æŽ¥ç¶šçµæžœã®å…¥åŠ›ã‚’å‡ºåŠ›ã—ã¾ã™ã? 487 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 488 */ 489 public InputStream getInputStream() throws IOException { 490 // InputStream in = new BufferedInputStream( conn.getInputStream() ); 491 // return in; 492 return new BufferedInputStream( conn.getInputStream() ); // 5.5.2.4 (2012/05/16) 493 } 494 495 /** 496 * HttpURLConnection ã®ãƒ¬ã‚¹ãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸æ–?—å?ã‚’è¿”ã—ã¾ã™ã? 497 * 498 * HttpURLConnection ã® getResponseCode() メソãƒ?ƒ‰ã«ã‚ˆã‚Šå–å¾—ã•れãŸã€HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ? 499 * ã«å¯¾å¿œã™ã‚‹æ–‡å—å?ã‚’è¿”ã—ã¾ã™ã?ã“ã?æ–?—å?ã¯ã€HttpURLConnection ã§å®šç¾©ã•れã? 500 * static 定数ã®ã‚³ãƒ¡ãƒ³ãƒˆã‚’ã€å®šç¾©ã—ã¦ã?¾ã™ã? 501 * 502 * @og.rev 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ä¾?追åŠ? 503 * 504 * @param code HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ? 505 * 506 * @return レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹æ–‡å—å? 507 * @see HttpURLConnection#HTTP_ACCEPTED 508 */ 509 public static String code2Message( final int code ) { 510 final String msg ; 511 switch( code ) { 512 case 100 : msg = "100: è¦æ±‚ã?続行å¯èƒ½ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 513 case 101 : msg = "101: プãƒãƒˆã‚³ãƒ«ã‚’å?り替ãˆã¾ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 514 case HttpURLConnection.HTTP_OK : msg = "200: OK ã§ã™ã?" ; break; 515 case HttpURLConnection.HTTP_CREATED : msg = "201: 作æ?ã•れã¾ã—ãŸã€? ; break; 516 case HttpURLConnection.HTTP_ACCEPTED : msg = "202: 許å¯ã•れã¾ã—ãŸã€? ; break; 517 case HttpURLConnection.HTTP_NOT_AUTHORITATIVE : msg = "203: ä¸å½“ãªæƒ??ã§ã™ã?" ; break; 518 case HttpURLConnection.HTTP_NO_CONTENT : msg = "204: コンãƒ?ƒ³ãƒ?Œã‚りã¾ã›ã‚“ã€? ; break; 519 case HttpURLConnection.HTTP_RESET : msg = "205: コンãƒ?ƒ³ãƒ?‚’リセãƒ?ƒˆã—ã¾ã™ã?" ; break; 520 case HttpURLConnection.HTTP_PARTIAL : msg = "206: 部åˆ?š„ãªã‚³ãƒ³ãƒ?ƒ³ãƒ?§ã™ã?" ; break; 521 case HttpURLConnection.HTTP_MULT_CHOICE : msg = "300: è¤?•°é¸æŠžã•れã¦ã?¾ã™ã?" ; break; 522 case HttpURLConnection.HTTP_MOVED_PERM : msg = "301: 永続的ã«ç§»å‹•ã•れã¾ã—ãŸã€? ; break; 523 case HttpURLConnection.HTTP_MOVED_TEMP : msg = "302: ä¸?™‚çš?«åˆ?‚Šæ›¿ãˆã¾ã™ã?" ; break; 524 case HttpURLConnection.HTTP_SEE_OTHER : msg = "303: ä»–ã‚’å‚ç?ã—ã¦ãã ã•ã„ã€? ; break; 525 case HttpURLConnection.HTTP_NOT_MODIFIED : msg = "304: ä¿®æ£ã•れã¾ã›ã‚“ã§ã—ãŸã€? ; break; 526 case HttpURLConnection.HTTP_USE_PROXY : msg = "305: プãƒã‚シを使用ã—ã¦ãã ã•ã„ã€? ; break; 527 case 306 : msg = "306: 仕様ã?拡張案ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 528 case 307 : msg = "307: ä¸?™‚çš?ªãƒªãƒ?‚¤ãƒ¬ã‚¯ãƒˆã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 529 case HttpURLConnection.HTTP_BAD_REQUEST : msg = "400: ä¸å½“ãªè¦æ±‚ã§ã™ã?" ; break; 530 case HttpURLConnection.HTTP_UNAUTHORIZED : msg = "401: èªè¨¼ã•れã¾ã›ã‚“ã§ã—ãŸã€? ; break; 531 case HttpURLConnection.HTTP_PAYMENT_REQUIRED : msg = "402: 支払ã„ãŒå¿?¦ã§ã™ã?" ; break; 532 case HttpURLConnection.HTTP_FORBIDDEN : msg = "403: ç¦æ¢ã•れã¦ã?¾ã™ã?" ; break; 533 case HttpURLConnection.HTTP_NOT_FOUND : msg = "404: 見ã¤ã‹ã‚Šã¾ã›ã‚“ã§ã—ãŸã€? ; break; 534 case HttpURLConnection.HTTP_BAD_METHOD : msg = "405: メソãƒ?ƒ‰ã¯è¨±å¯ã•れã¾ã›ã‚“ã€? ; break; 535 case HttpURLConnection.HTTP_NOT_ACCEPTABLE : msg = "406: 許容ã•れã¾ã›ã‚“ã€? ; break; 536 case HttpURLConnection.HTTP_PROXY_AUTH : msg = "407: プãƒã‚ã‚·ã®èªè¨¼ãŒå¿?¦ã§ã™ã?" ; break; 537 case HttpURLConnection.HTTP_CLIENT_TIMEOUT : msg = "408: è¦æ±‚ãŒæ™‚é–“åˆ?‚Œã§ã™ã?" ; break; 538 case HttpURLConnection.HTTP_CONFLICT : msg = "409: é‡è¤?—ã¦ã?¾ã™ã?" ; break; 539 case HttpURLConnection.HTTP_GONE : msg = "410: å˜åœ¨ã—ã¾ã›ã‚“ã€? ; break; 540 case HttpURLConnection.HTTP_LENGTH_REQUIRED : msg = "411: é•·ã•ãŒå¿?¦ã§ã™ã?" ; break; 541 case HttpURLConnection.HTTP_PRECON_FAILED : msg = "412: 剿æ¡ä»¶ãŒæ£ã—ãã‚りã¾ã›ã‚“ã€? ; break; 542 case HttpURLConnection.HTTP_ENTITY_TOO_LARGE : msg = "413: è¦æ±‚エンãƒ?‚£ãƒ?‚£ãŒé•·ã™ãŽã¾ã™ã?" ; break; 543 case HttpURLConnection.HTTP_REQ_TOO_LONG : msg = "414: è¦æ±?URL ãŒé•·ã™ãŽã¾ã™ã?" ; break; 544 case HttpURLConnection.HTTP_UNSUPPORTED_TYPE : msg = "415: サãƒã?トã•れãªã?ƒ¡ãƒ?‚£ã‚¢ã‚¿ã‚¤ãƒ—ã§ã™ã?" ; break; 545 case 416 : msg = "416: è¦æ±‚ã•れãŸç¯?›²ã¯ä¸ååˆ?§ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 546 case 417 : msg = "417: è¦æ±‚ã©ãŠã‚Šã®å‡¦ç?Œä¸å¯èƒ½ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27) 547 case HttpURLConnection.HTTP_INTERNAL_ERROR : msg = "500: å†?ƒ¨ã‚µãƒ¼ãƒã‚¨ãƒ©ãƒ¼ã§ã™ã?" ; break; 548 case HttpURLConnection.HTTP_NOT_IMPLEMENTED : msg = "501: 実è£?•れã¦ã?¾ã›ã‚“ã€? ; break; 549 case HttpURLConnection.HTTP_BAD_GATEWAY : msg = "502: 誤ã£ãŸã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ã§ã™ã?" ; break; 550 case HttpURLConnection.HTTP_UNAVAILABLE : msg = "503: サービスãŒåˆ©ç”¨ã§ãã¾ã›ã‚“ã€? ; break; 551 case HttpURLConnection.HTTP_GATEWAY_TIMEOUT : msg = "504: ã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ãŒæ™‚é–“å?れã§ã™ã?" ; break; 552 case HttpURLConnection.HTTP_VERSION : msg = "505: HTTP ãƒã?ジョンãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€?; break; 553 // default : msg = "-1: 未定義" ; 554 default : msg = code + ": 未定義" ; // 5.6.7.0 (2013/07/27) 555 } 556 return msg ; 557 } 558 559 /** 560 * サンプル実行用ã®ãƒ¡ã‚¤ãƒ³ãƒ¡ã‚½ãƒ?ƒ‰ 561 * 562 * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd] 563 * 564 * args[*] : [-info/-data] æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)ã€? 565 * args[*] : [-post=ファイルå] POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)ã€? 566 * args[*] : [-encode=UTF-8] エンコードをæŒ?®šã—ã¾ã?é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用) 567 * args[*] : [-out=ファイルå] çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã™ã? 568 * args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)ã€? 569 * args[A] : url ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã™ã? 570 * args[B] : [user:passwd] BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã™ã? 571 * 572 * @og.rev 5.6.7.0 (2013/07/27) -errEx 追åŠ? 573 * 574 * @param args コマンド引数é…å? 575 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã? 576 */ 577 public static void main( final String[] args ) throws IOException { 578 if( args.length < 3 ) { 579 LogWriter.log( "Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]" ); 580 LogWriter.log( " args[*] : [-info/-data] æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)" ); 581 LogWriter.log( " args[*] : [-post=ファイルå] POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)" ); 582 LogWriter.log( " args[*] : [-encode=UTF-8] エンコードをæŒ?®šã—ã¾ã™ã?(é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)" ); 583 LogWriter.log( " args[*] : [-out=ファイルå] çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã? ); 584 LogWriter.log( " args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)" ); 585 LogWriter.log( " args[A] : url ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã? ); 586 LogWriter.log( " args[B] : [user:passwd] BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã? ); 587 return; 588 } 589 590 boolean isInfo = false ; 591 boolean isPost = false ; 592 String postKey = null ; 593 String postFile = null ; 594 String encode = null ; 595 String outFile = null ; 596 boolean isEx = false ; // 5.6.7.0 (2013/07/27) 追åŠ? 597 String[] vals = new String[2]; // url,userPass ã®é ?«å¼•æ•°è¨å®? 598 599 int adrs = 0; 600 for( int i=0; i<args.length; i++ ) { 601 String arg = args[i]; 602 if( arg.equalsIgnoreCase( "-info" ) ) { 603 isInfo = true; 604 } 605 else if( arg.equalsIgnoreCase( "-data" ) ) { 606 isInfo = false; 607 } 608 else if( arg.startsWith( "-post=" ) ) { 609 isPost = true; 610 int sepAdrs = arg.indexOf( ':',6 ); 611 postKey = arg.substring( 6,sepAdrs ); 612 postFile = arg.substring( sepAdrs+1 ); 613 } 614 else if( arg.startsWith( "-encode=" ) ) { 615 encode = arg.substring( 8 ); 616 } 617 else if( arg.startsWith( "-out=" ) ) { 618 outFile = arg.substring( 5 ); 619 } 620 else if( arg.startsWith( "-errEx=" ) ) { // 5.6.7.0 (2013/07/27) 追åŠ? 621 isEx = "true".equalsIgnoreCase( arg.substring( 7 ) ); 622 } 623 else if( arg.startsWith( "-" ) ) { 624 System.out.println( "Error Argment:" + arg ); 625 } 626 else { 627 vals[adrs++] = arg; 628 } 629 } 630 631 String urlStr = vals[0] ; 632 String userPass = vals[1] ; 633 634 URLConnect conn = new URLConnect( urlStr,userPass ); 635 636 // POST ãƒ??ã‚¿ã¯ã€connect() ã™ã‚‹å‰ã«ã€è¨å®šã—ã¾ã™ã? 637 if( isPost ) { 638 FileString file = new FileString(); 639 file.setFilename( postFile ); 640 String postData = file.getValue(); 641 642 conn.setPostData( XHTMLTag.urlEncode(postKey, postData) ); 643 } 644 645 conn.connect(); 646 if( encode != null ) { 647 conn.setCharset( encode ); // encode æŒ?®? 648 } 649 else { 650 encode = conn.getCharset(); // æŒ?®šãŒãªã‘れã°ã€æŽ¥ç¶šå?ã® charset を使用 651 } 652 653 final PrintWriter writer ; 654 if( outFile != null ) { 655 writer = FileUtil.getPrintWriter( new File( outFile ),encode ); 656 } 657 else { 658 writer = FileUtil.getLogWriter( "System.out" ); 659 } 660 661 int code = conn.getCode(); // 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã?ã€å¸¸ã«æ‹¾ã£ã¦ãŠãã¾ã™ã? 662 if( isInfo ) { 663 writer.println( "URL :" + conn.getUrl() ); 664 writer.println( "Type :" + conn.getType() ); 665 // writer.println( "Code :" + conn.getCode() ); 666 writer.println( "Code :" + code ); // 5.6.7.0 (2013/07/27) å–得済ã¿ã®å€¤ã‚’利用ã€? 667 writer.println( "Message:" + conn.getMessage() ); 668 writer.println( "Charset:" + conn.getCharset() ); 669 } 670 else { 671 writer.println( conn.readData() ); 672 } 673 674 conn.disconnect(); 675 676 Closer.ioClose( writer ); 677 678 // 5.6.7.0 (2013/07/27) trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã? 679 if( isEx && code >= 400 ) { 680 String errMsg = URLConnect.code2Message( code ); 681 throw new RuntimeException( errMsg ); 682 } 683 } 684 }