在Linux

时间:2015-05-25 06:53:28

标签: linux http-post fiddler latency simulate

使用案例

我正在开发企业级支付应用程序(用JAVA编写)。我希望模拟对银行进行的HTTP POST调用的延迟。这将允许我模拟可能发生的不同延迟/不可用场景。

代码

以下代码将请求发送给银行:

try {
    // Set the location of the Bank Of America payment gateway
    URL url = new URL(getParameterGatewayUrl());

    // Open the connection
    urlConnection = url.openConnection();

    // Set the connection timeout
    urlConnection.setConnectTimeout(getTimeoutSeconds() * 1000);

    // Set the DoOutput flag to true because we intend
    // to use the URL connection for output
    urlConnection.setDoOutput(true);

    // Send the transaction via HTTPS POST
    OutputStream outputStream = urlConnection.getOutputStream();
    outputStream.write(postVars.getBytes());
    outputStream.close();
} catch (TimeoutException exception){

}

try {
    //Set the read timeout
    urlConnection.setReadTimeout(getTimeoutSeconds() * 1000);

    // Get the response from Bank Of America
    InputStream inputStream = urlConnection.getInputStream();
    while ((inputStreamCharacter = inputStream.read()) != -1)
        responseText.append((char) inputStreamCharacter);

    inputStream.close();
    LOG.debug("Bank Of America responseText: " + responseText);
} catch (SocketTimeoutException exception){

}

这段代码在异步付款任务中运行。

方案

以下是发生的事情:

  1. 我们与银行的支付网关建立连接
  2. 我们向银行发送付款申请。
  3. 我们等待银行向我们发送回复请求。
  4. 我们收到回复并解析并检查是否成功。
  5. 现在,我们希望在请求发送到银行之后,即在我们收到银行的回复之前模拟延迟。这样在第二个try / catch块中引发了 SocketTimeoutException 异常。

    问题

    我需要处理的应用程序实例托管在运行 14.04.1-Ubuntu 的服务器VM上。我过去在Windows托管的应用程序上使用了Fiddlerintroduce latency。但棘手的是,Fiddler是一个基于UI的程序,我无法在Linux shell上使用它。 此外,我们没有从银行得到太多帮助。否则,如果在服务器端而不是在客户端模拟这一切,那将会容易得多。

    问题

    我用Google搜索,但未能找到解决方法。有没有人尝试过这些方法?如果是这样我们怎么做呢?任何建议都会受到欢迎。

2 个答案:

答案 0 :(得分:2)

我已经能够找到一种解决方法来测试它。我从this answer获得了帮助。虽然httpbin是一个了不起的项目,但却无法延迟POST请求的响应。因此,我分叉了他们的存储库,并自己添加了所需的端点。 fork适用于任何需要它的人。

现在,人们可以通过 / post / delay / 将网关URL更改为基于httpbin的URL,结果将生成延迟的POST请求响应。

答案 1 :(得分:0)

Fiddler是一个代理服务器;您只需在一台计算机(任何地方)上运行它,并将Linux客户端的代理设置指向它。

有关详细信息,请参阅Monitor Remote requests

相关问题