扩展HttpsURLConnection

时间:2017-01-29 17:03:29

标签: java httpurlconnection

我想为HttpsURLConnection添加功能,所以我尝试像这样扩展这个类:

public abstract class HTTPClient extends HttpsURLConnection

当我尝试使用这个课时:

HTTPClient con = (HTTPClient) url.openConnection();

我收到以下错误:

Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to HTTPClient

这里有什么问题以及如何解决?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以扩展如下:

class H extends HttpURLConnection {
  URLConnection uc;
  public H(URL url) {
    super(url);
    try { uc=url.openConnection(); } catch(Exception ex) { ex.printStackTrace();  }
 }

 public boolean usingProxy() { return true; }

 public void disconnect() { }

 public void connect() { }

}