gpt4 book ai didi

java - 模拟 java.net.ConnectException : Connection timout

转载 作者:可可西里 更新时间:2023-11-01 16:37:20 25 4
gpt4 key购买 nike

我想在我的 java 应用程序中人为地创建一个连接超时来测试我的连接超时处理。我看过这个帖子 Artificially create a connection timeout error .

但是当我尝试建议的内容时,例如 http://10.255.255.1http://www.google.com:81我用来进行 http 调用的库(Apache http 客户端 3.1)给了我一个连接被拒绝的异常而不是超时。

[9/07/15 19:28:45:000 EST] 00000088 SystemErr     R IOException Connection refused: connect
[9/07/15 19:28:46:188 EST] 00000088 SystemErr R java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:336)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:201)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
at java.net.Socket.connect(Socket.java:478)
at sun.reflect.GeneratedMethodAccessor187.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:140)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)

有人可以建议我可以调整什么或不同的建议一起让我测试连接超时吗?


编辑

这是我要测试的代码....

    HttpClient client = new HttpClient();           
HttpConnectionManagerParams httpConnectionManagerParams = client.getHttpConnectionManager().getParams();
httpConnectionManagerParams.setConnectionTimeout(45000);
httpConnectionManagerParams.setSoTimeout(45000);
httpConnectionManagerParams.setTcpNoDelay(false);

// Create a method instance.
GetMethod method = new GetMethod(url);
HttpMethodParams httpMethodParams = method.getParams();

// no retries
httpMethodParams.setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
httpMethodParams.setSoTimeout(45000);

try {
// Execute the method.
int statusCode = client.executeMethod(method);

// Read the response body.
byte[] responseBody = method.getResponseBody();
jsonResponse = new String(
new String(responseBody).getBytes(JsonConstants.DEFAULT_CHARSET), JsonConstants.DEFAULT_CHARSET);

} catch (HttpException e) {
System.err.println("HttpException " + e.getMessage());
e.printStackTrace();

} catch (IOException e) {
System.err.println("IOException " + e.getMessage());
e.printStackTrace();

} finally {
// Release the connection.
method.releaseConnection();
}

谢谢

最佳答案

为什么不使用模拟测试?恕我直言,它是最好的模拟工具之一:JMockit .您可以模拟,例如 HttpClient 类并抛出 connectionTimeOutException(或任何您真正需要测试代码的东西)。

根据您发送的代码,我仍然不清楚超时发生时会发生什么。它会抛出异常吗?当它发生时,您的代码如何识别它?

请看一下。如果您需要进一步的帮助,请告诉我 =]

关于java - 模拟 java.net.ConnectException : Connection timout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com