1 7 8 package java.net; 9 10 import java.io.InputStream ; 11 import java.io.IOException ; 12 import java.security.Permission ; 13 import java.util.Date ; 14 15 33 abstract public class HttpURLConnection extends URLConnection { 34 35 36 39 protected String method = "GET"; 40 41 45 protected int chunkLength = -1; 46 47 52 protected int fixedContentLength = -1; 53 54 65 public String getHeaderFieldKey (int n) { 66 return null; 67 } 68 69 98 public void setFixedLengthStreamingMode (int contentLength) { 99 if (connected) { 100 throw new IllegalStateException ("Already connected"); 101 } 102 if (chunkLength != -1) { 103 throw new IllegalStateException ("Chunked encoding streaming mode set"); 104 } 105 if (contentLength < 0) { 106 throw new IllegalArgumentException ("invalid content length"); 107 } 108 fixedContentLength = contentLength; 109 } 110 111 115 private static final int DEFAULT_CHUNK_SIZE = 4096; 116 117 141 public void setChunkedStreamingMode (int chunklen) { 142 if (connected) { 143 throw new IllegalStateException ("Can't set streaming mode: already connected"); 144 } 145 if (fixedContentLength != -1) { 146 throw new IllegalStateException ("Fixed length streaming mode set"); 147 } 148 chunkLength = chunklen <=0? DEFAULT_CHUNK_SIZE : chunklen; 149 } 150 151 166 public String getHeaderField(int n) { 167 return null; 168 } 169 170 180 protected int responseCode = -1; 181 182 185 protected String responseMessage = null; 186 187 188 189 190 private static boolean followRedirects = true; 191 192 208 protected boolean instanceFollowRedirects = followRedirects; 209 210 211 private static final String [] methods = { 212 "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE" 213 }; 214 215 219 protected HttpURLConnection (URL u) { 220 super(u); 221 } 222 223 241 public static void setFollowRedirects(boolean set) { 242 SecurityManager sec = System.getSecurityManager(); 243 if (sec != null) { 244 sec.checkSetFactory(); 246 } 247 followRedirects = set; 248 } 249 250 259 public static boolean getFollowRedirects() { 260 return followRedirects; 261 } 262 263 277 public void setInstanceFollowRedirects(boolean followRedirects) { 278 instanceFollowRedirects = followRedirects; 279 } 280 281 290 public boolean getInstanceFollowRedirects() { 291 return instanceFollowRedirects; 292 } 293 294 312 public void setRequestMethod(String method) throws ProtocolException { 313 if (connected) { 314 throw new ProtocolException ("Can't reset method: already connected"); 315 } 316 321 for (int i = 0; i < methods.length; i++) { 322 if (methods[i].equals(method)) { 323 this.method = method; 324 return; 325 } 326 } 327 throw new ProtocolException ("Invalid HTTP method: " + method); 328 } 329 330 335 public String getRequestMethod() { 336 return method; 337 } 338 339 352 public int getResponseCode() throws IOException { 353 356 if (responseCode != -1) { 357 return responseCode; 358 } 359 360 365 Exception exc = null; 366 try { 367 getInputStream(); 368 } catch (Exception e) { 369 exc = e; 370 } 371 372 376 String statusLine = getHeaderField(0); 377 if (statusLine == null) { 378 if (exc != null) { 379 if (exc instanceof RuntimeException ) 380 throw (RuntimeException )exc; 381 else 382 throw (IOException )exc; 383 } 384 return -1; 385 } 386 387 395 if (statusLine.startsWith("HTTP/1.")) { 396 int codePos = statusLine.indexOf(' '); 397 if (codePos > 0) { 398 399 int phrasePos = statusLine.indexOf(' ', codePos+1); 400 if (phrasePos > 0 && phrasePos < statusLine.length()) { 401 responseMessage = statusLine.substring(phrasePos+1); 402 } 403 404 if (phrasePos < 0) 407 phrasePos = statusLine.length(); 408 409 try { 410 responseCode = Integer.parseInt 411 (statusLine.substring(codePos+1, phrasePos)); 412 return responseCode; 413 } catch (NumberFormatException e) { } 414 } 415 } 416 return -1; 417 } 418 419 432 public String getResponseMessage() throws IOException { 433 getResponseCode(); 434 return responseMessage; 435 } 436 437 public long getHeaderFieldDate(String name, long Default) { 438 String dateString = getHeaderField(name); 439 try { 440 dateString.trim(); 441 if (dateString.indexOf("GMT") == -1) { 442 dateString = dateString+" GMT"; 443 } 444 return Date.parse(dateString); 445 } catch (Exception e) { 446 } 447 return Default; 448 } 449 450 451 457 public abstract void disconnect(); 458 459 464 public abstract boolean usingProxy(); 465 466 public Permission getPermission() throws IOException { 467 int port = url.getPort(); 468 port = port < 0 ? 80 : port; 469 String host = url.getHost() + ":" + port; 470 Permission permission = new SocketPermission (host, "connect"); 471 return permission; 472 } 473 474 492 public InputStream getErrorStream() { 493 return null; 494 } 495 496 499 500 503 504 505 508 public static final int HTTP_OK = 200; 509 510 513 public static final int HTTP_CREATED = 201; 514 515 518 public static final int HTTP_ACCEPTED = 202; 519 520 523 public static final int HTTP_NOT_AUTHORITATIVE = 203; 524 525 528 public static final int HTTP_NO_CONTENT = 204; 529 530 533 public static final int HTTP_RESET = 205; 534 535 538 public static final int HTTP_PARTIAL = 206; 539 540 541 542 545 public static final int HTTP_MULT_CHOICE = 300; 546 547 550 public static final int HTTP_MOVED_PERM = 301; 551 552 555 public static final int HTTP_MOVED_TEMP = 302; 556 557 560 public static final int HTTP_SEE_OTHER = 303; 561 562 565 public static final int HTTP_NOT_MODIFIED = 304; 566 567 570 public static final int HTTP_USE_PROXY = 305; 571 572 573 574 577 public static final int HTTP_BAD_REQUEST = 400; 578 579 582 public static final int HTTP_UNAUTHORIZED = 401; 583 584 587 public static final int HTTP_PAYMENT_REQUIRED = 402; 588 589 592 public static final int HTTP_FORBIDDEN = 403; 593 594 597 public static final int HTTP_NOT_FOUND = 404; 598 599 602 public static final int HTTP_BAD_METHOD = 405; 603 604 607 public static final int HTTP_NOT_ACCEPTABLE = 406; 608 609 612 public static final int HTTP_PROXY_AUTH = 407; 613 614 617 public static final int HTTP_CLIENT_TIMEOUT = 408; 618 619 622 public static final int HTTP_CONFLICT = 409; 623 624 627 public static final int HTTP_GONE = 410; 628 629 632 public static final int HTTP_LENGTH_REQUIRED = 411; 633 634 637 public static final int HTTP_PRECON_FAILED = 412; 638 639 642 public static final int HTTP_ENTITY_TOO_LARGE = 413; 643 644 647 public static final int HTTP_REQ_TOO_LONG = 414; 648 649 652 public static final int HTTP_UNSUPPORTED_TYPE = 415; 653 654 655 656 660 @Deprecated 661 public static final int HTTP_SERVER_ERROR = 500; 662 663 666 public static final int HTTP_INTERNAL_ERROR = 500; 667 668 671 public static final int HTTP_NOT_IMPLEMENTED = 501; 672 673 676 public static final int HTTP_BAD_GATEWAY = 502; 677 678 681 public static final int HTTP_UNAVAILABLE = 503; 682 683 686 public static final int HTTP_GATEWAY_TIMEOUT = 504; 687 688 691 public static final int HTTP_VERSION = 505; 692 693 } 694 | Popular Tags |