⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpsurlconnection.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        } catch(AuthenticationCancelledException ex) {
          throw new IOException("The user cancelled proxy authentication!");
        }
      }
      else {
        String host = url.getHost();
        if (host == null) {
          throw new IOException("No host specified.");
        }
        int port = url.getPort();
        try {
          socket = new SSLSocket(host, port == -1 ? 443 : port);
        }
        catch (SSLException ex1) {
          throw new SSLIOException(ex1);
        }
      }

      try {
        writeRequest(socket.getOutputStream());
        readResponse(input = new PushbackInputStream(socket.getInputStream(), 2048));
      }
      catch (IOException ex) {
        try {
          socket.close();
        }
        catch (IOException ignored) {}
        throw ex;
      }
      connected = true;
    }
  }

  /*void writeProxyRequest(OutputStream out) throws IOException {
    DataOutputStream data =
        new DataOutputStream(new BufferedOutputStream(out));
    String host = url.getHost();
    if (host == null) {
      throw new IOException("No host specified.");
    }
    int port = url.getPort();

    data.writeBytes("CONNECT " + host + ":" + port + " HTTP/1.0\r\n");
    data.writeBytes("User-agent: " +
                    getDefaultRequestProperty("User-agent") + "\r\n");
    for (int i = 0; i < requestKeys.size(); ++i) {
      String key = (String) requestKeys.elementAt(i);
      if (key.startsWith("Proxy-")) {
        data.writeBytes(key + ": " + requestValues.elementAt(i) + "\r\n");
      }
    }
    data.writeBytes("\n");
    data.flush();
  }

  void readProxyResponse(InputStream in) throws IOException {
    DataInputStream data = new DataInputStream(in); // Can't buffer
    Vector proxyHeaders = new Vector();
    String line;
    while ( ( (line = data.readLine()) != null) && (line.length() > 0)) {
      proxyHeaders.addElement(line);
    }
    String response = (String) proxyHeaders.elementAt(0);
    int index = response.indexOf(' ');
    while (response.charAt(++index) == ' ') {
      ;
    }
    int responseCode =
        Integer.parseInt(response.substring(index, index + 3));
    if (responseCode != 200) {
      throw new IOException("Proxy connection failed: " + response);
    }
  }*/

  void writeRequest(OutputStream out) throws IOException {
    DataOutputStream data = new DataOutputStream(
        new BufferedOutputStream(out));
    if ( (doOutput) && (output == null)) {
      throw new IOException("No POST data specified");
    }
    if (ifModifiedSince != 0) {
      Date date = new Date(ifModifiedSince);
      SimpleDateFormat formatter =
          new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z");
      formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
      setRequestProperty("If-Modified-Since", formatter.format(date));
    }
    if (doOutput) {
      setRequestProperty("Content-length", "" + output.size());
    }

    data.writeBytes
        ( (doOutput ? "POST" : "GET") + " " + ( url.getFile().equals("") ? "/" : url.getFile() ) + " HTTP/1.0\r\n" );

    for (int i = 0; i < requestKeys.size(); ++i) {
      String key = (String) requestKeys.elementAt(i);
      if (!key.startsWith("Proxy-")) {
        data.writeBytes(key + ": " + requestValues.elementAt(i) + "\r\n");
      }
    }
    data.writeBytes("\r\n");
    data.flush();
    if (doOutput) {
      output.writeTo(out);
    }
    out.flush();
  }

  void readResponse(PushbackInputStream in) throws IOException {
    DataInputStream data = new DataInputStream(in);
    String line;
    while ( ( (line = data.readLine()) != null) &&
           (line.length() > 0)) {
      headers.addElement(line);
      int index = line.indexOf(':');
      if (index >= 0) {
        String key = line.substring(0, index);
        String value = line.substring(index + 1).trim();
        headerKeys.addElement(key);
        headerValues.put(key.toLowerCase(), value);
      }
      else {
        //	If the first line back is not a header, the unread as the rest is going to be content
        if(headerValues.size() == 0) {

          // This is a response code
          if(line.startsWith("HTTP/")) {
		      try {
		        int idx = line.indexOf(' ');
			    while (line.charAt(++idx) == ' ') {
			        ;
			    }
			    responseMessage = line.substring(idx + 4);
		        responseCode = Integer.parseInt(line.substring(idx, idx + 3));
		      }
		      catch(Throwable t) {
		        responseCode = 200;
		      }
          }
          else {
            // Just content
            responseCode = 200;
            byte[] unread = line.getBytes();
            in.unread(unread);
            break;
          }

        }
      }
    }
  }

  public int getResponseCode() {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
//    if (responseCode == -1) {
//      responseCode = 200; // Its possible that there wont be a response code
//      if(headers.size() > 0) {
//	      String response = getHeaderField(0);
//	      System.out.println("!!!!! REMOVE ME maverick/src/com/maverick/ssl/https/HttpsURLConnection.getResponseCode() - response = " + response);
//	      int index = response.indexOf(' ');
//	      while (response.charAt(++index) == ' ') {
//	        ;
//	      }
//	      responseMessage = response.substring(index + 4);
//	      try {
//	        responseCode = Integer.parseInt(response.substring(index, index + 3));
//	      }
//	      catch(NumberFormatException nfe) {
//	        // ????
//	      }
//      }
//    }
    return responseCode;
  }

  public String getResponseMessage() {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
    getResponseCode();
    return responseMessage;
  }

  public String getHeaderField(String name) {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
    return (String) headerValues.get(name.toLowerCase());
  }

  public String getHeaderFieldKey(int n) {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
    if (n < headerKeys.size()) {
      return (String) headerKeys.elementAt(n);
    }
    else {
      return null;
    }
  }

  public String getHeaderField(int n) {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
    if (n < headers.size()) {
      return (String) headers.elementAt(n);
    }
    else {
      return null;
    }
  }

  public InputStream getInputStream() throws IOException {
    if (!doInput) {
      throw new IOException("Protocol input not configured.");
    }
    connect();
    return input;
  }

  public Socket getSocket() {
    if (!connected) {
      throw new IllegalStateException("Not connected");
    }
    return socket;
  }

  public void disconnect() {

  }

  public boolean usingProxy() {
    return System.getProperty(httpProxyHostProperty) != null;
  }
}

⌨️ 快捷键说明

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