相关疑难解决方法(0)

使用java类HttpsURLConnection

我有一小段代码基本上要求HTTP-Client,即它的POSTS请求,并与re RESPONSE一起工作.只要HTTP被认为是更好的工作.出于某种原因,我现在也必须支持HTTPS.所以这里简要介绍一下如何打开连接:

 URL url = new URL(serverAddress);
 HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
Run Code Online (Sandbox Code Playgroud)

这失败了,说:

sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection
Run Code Online (Sandbox Code Playgroud)

我想这有点微不足道,但我只是不知道我在这个问题上做错了...用Google搜索,代码看起来是对的 - 不是吗?

任何想法都表示赞赏!

java https

32
推荐指数
6
解决办法
8万
查看次数

如何模拟URL连接

嗨,我有一个方法,将URL作为输入,并确定它是否可访问.下面是代码:

public static boolean isUrlAccessible(final String urlToValidate) throws WAGNetworkException {
        URL url = null;
        HttpURLConnection huc = null;
        int responseCode = -1;
        try {
            url = new URL(urlToValidate);
            huc = (HttpURLConnection) url.openConnection();
            huc.setRequestMethod("HEAD");
            huc.connect();
            responseCode = huc.getResponseCode();
        } catch (final UnknownHostException e) {
            throw new WAGNetworkException(WAGConstants.INTERNET_CONNECTION_EXCEPTION);
        } catch (IOException e) {
            throw new WAGNetworkException(WAGConstants.INVALID_URL_EXCEPTION);
        } finally {
            if (huc != null) {
                huc.disconnect();
            }
        }
        return responseCode == 200;
    }
Run Code Online (Sandbox Code Playgroud)

我想使用isUrlAccessible()方法对单元进行单元测试PowerMockito.我觉得我需要用来whenNew()模拟创建URLurl.openConnection()调用的时候,返回另一个模拟HttpURLConnection …

java junit jmockit mocking powermock

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×2

https ×1

jmockit ×1

junit ×1

mocking ×1

powermock ×1