httpurlconnection.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 654 行 · 第 1/2 页

JAVA
654
字号
            if (response.isError())	      errorSink = responseSink;          }      }    while (retry);    connected = true;  }    /**   * Returns a connection, from the pool if necessary.   */  HTTPConnection getConnection(String host, int port, boolean secure)    throws IOException  {    HTTPConnection connection;    if (keepAlive)      {        connection = HTTPConnection.Pool.instance.get(host, port, secure);      }    else      {        connection = new HTTPConnection(host, port, secure);      }    return connection;  }  public void disconnect()  {    if (connection != null)      {        try          {            connection.close();          }        catch (IOException e)          {          }      }  }  public boolean usingProxy()  {    return (proxyHostname != null);  }  /**   * Overrides the corresponding method in HttpURLConnection to permit   * arbitrary methods, as long as they're valid ASCII alphabetic   * characters. This is to permit WebDAV and other HTTP extensions to   * function.   * @param method the method   */  public void setRequestMethod(String method)    throws ProtocolException  {    if (connected)      {        throw new ProtocolException("Already connected");      }    // Validate    method = method.toUpperCase();    int len = method.length();    if (len == 0)      {        throw new ProtocolException("Empty method name");      }    for (int i = 0; i < len; i++)      {        char c = method.charAt(i);        if (c < 0x41 || c > 0x5a)          {            throw new ProtocolException("Illegal character '" + c +                                        "' at index " + i);          }      }    // OK    this.method = method;    requestMethodSetExplicitly = true;  }  public String getRequestProperty(String key)  {        return requestHeaders.getValue(key);  }  public Map getRequestProperties()  {    if (connected)      throw new IllegalStateException("Already connected");        Map m = requestHeaders.getAsMap();    return Collections.unmodifiableMap(m);  }  public void setRequestProperty(String key, String value)  {    super.setRequestProperty(key, value);        requestHeaders.put(key, value);  }  public void addRequestProperty(String key, String value)  {    super.addRequestProperty(key, value);    requestHeaders.addValue(key, value);  }  public OutputStream getOutputStream()    throws IOException  {    if (connected)      {        throw new ProtocolException("Already connected");      }    if (!doOutput)      {        throw new ProtocolException("doOutput is false");      }    else if (!requestMethodSetExplicitly)      {        /*         * Silently change the method to POST if no method was set         * explicitly. This is due to broken applications depending on this         * behaviour (Apache XMLRPC for one).         */        method = "POST";      }    if (requestSink == null)      {        requestSink = new ByteArrayOutputStream();      }    return requestSink;  }    // -- Response --    public InputStream getInputStream()    throws IOException  {    if (!connected)      {        connect();      }    if (!doInput)      {        throw new ProtocolException("doInput is false");      }        if (response.isError())      {        int code = response.getCode();        if (code == 404 || code == 410)          throw new FileNotFoundException(url.toString());              throw new IOException("Server returned HTTP response code " + code                              + " for URL " + url.toString());      }        return responseSink;  }  public InputStream getErrorStream()  {    return errorSink;  }  public Map getHeaderFields()  {    if (!connected)      {        try          {            connect();          }        catch (IOException e)          {            return null;          }      }    Map m = response.getHeaders().getAsMap();    m.put(null, Collections.singletonList(getStatusLine(response)));    return Collections.unmodifiableMap(m);  }  String getStatusLine(Response response)  {    return "HTTP/" + response.getMajorVersion() +      "." + response.getMinorVersion() +      " " + response.getCode() +      " " + response.getMessage();  }    public String getHeaderField(int index)  {    if (!connected)      {        try          {            connect();          }        catch (IOException e)          {            return null;          }      }    if (index == 0)      {        return getStatusLine(response);      }    return response.getHeaders().getHeaderValue(index - 1);  }  public String getHeaderFieldKey(int index)  {    if (!connected)      {        try          {            connect();          }        catch (IOException e)          {            return null;          }      }    // index of zero is the status line.    return response.getHeaders().getHeaderName(index - 1);  }  public String getHeaderField(String name)  {    if (!connected)      {        try          {            connect();          }        catch (IOException e)          {            return null;          }      }    return response.getHeader(name);  }  public long getHeaderFieldDate(String name, long def)  {    if (!connected)      {        try          {            connect();          }        catch (IOException e)          {            return def;          }      }    Date date = response.getDateHeader(name);    return (date == null) ? def : date.getTime();  }  public String getContentType()  {    return getHeaderField("Content-Type");  }  public int getResponseCode()    throws IOException  {    if (!connected)      {        connect();      }    return response.getCode();  }  public String getResponseMessage()    throws IOException  {    if (!connected)      {        connect();      }    return response.getMessage();  }  // -- HTTPS specific --  public String getCipherSuite()  {    if (!connected)      {        throw new IllegalStateException("not connected");      }    return handshakeEvent.getCipherSuite();  }    public Certificate[] getLocalCertificates()  {    if (!connected)      {        throw new IllegalStateException("not connected");      }    return handshakeEvent.getLocalCertificates();  }  public Certificate[] getServerCertificates()    throws SSLPeerUnverifiedException  {    if (!connected)      {        throw new IllegalStateException("not connected");      }    return handshakeEvent.getPeerCertificates();  }  // HandshakeCompletedListener  public void handshakeCompleted(HandshakeCompletedEvent event)  {    handshakeEvent = event;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?