htmlunit-2.7 - java.net.SocketException:连接重置

时间:2017-07-18 14:35:07

标签: java soapui htmlunit

我正在尝试使用htmlunit-2.7库(这是SoapUI-5.2.1中可用的默认库)来自动化用户级令牌检索过程(OAuth2的授权代码授权流程)。 但是当我尝试通过WebClient类htmlunit-2.7连接到Google Page时,我收到了连接重置错误。

以下是我的代码,

我在项目中使用的依赖Jar文件

公地编解码器-1.3.jar 公地httpclient的-3.1.jar 公地IO-2.4.jar 公地郎2.4.jar 共享记录-1.1.1.jar 的HtmlUnit-2.7.jar 的HtmlUnit核心JS-2.7.jar SAC-1.3.jar xercesImpl-2.9.1.jar

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class HTMLUnitClass 
{

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub


        System.out.println("Starting the  Script");

        try
         {
          //Webpage Login  
            WebClient driver = new WebClient();
            System.out.println("Instantiating the WebClient");


            driver.setJavaScriptEnabled(true);
            System.out.println("Enabling the Java Script");
            driver.setThrowExceptionOnScriptError(false);
            System.out.println("Disabling the Throw Exception on Script Error");


            try
            {
                driver.getPage("https://www.google.com");
                System.out.println("Getting the Google Page");

            }
            catch(Exception e)
            {
                System.out.println("Exception in the HTML Unit Driver Block: " + e);
            }


            }
            catch (Exception e)
            {
                System.out.println("Exception in the HTTPClient Block" + e);
            }




    }

}

我收到以下错误 - 下面的堆栈,

Starting the Script
Instantiating the HTMLUnit Driver
Enabling the Java Script
Disabling the Throw Exception on Script Error
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset
Jul 18, 2017 6:34:41 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
Exception in the HTML Unit Driver Block: java.net.SocketException: Connection reset
  

请注意-1:   请注意,我不能使用最新的HtmlUnitDriver库   (Jar),因为这会导致版本与默认冲突   SoapUI-5.2.1的库这就是我坚持的原因   安装包中提供的默认库   SoapUI-5.2.1并尝试将此解决方案与Groovy集成   脚本在SoapUI的一个测试步骤中可用。

     

注意2:我也尝试过使用Apache OAuth Client 2.0库   自动生成级别令牌生成(OpenText()

有人可以解释如何解决此连接重置问题吗?

1 个答案:

答案 0 :(得分:0)

我自己遇到了同样的问题,正确的解决方案是@ canpan14在第一个评论中提出的解决方案

我解决该错误的方法如下:

WebClient webClient = new WebClient(BEST_SUPPORTED);
webClient.getOptions().setUseInsecureSSL(true); //ignore insecure ssl exception
webClient.getOptions().setSSLClientProtocols(new String[] { "TLSv1.2","TLSv1.1" });

最后一行代码实际上设置了要使用的TLS类型。首选使用TLSv1.2,以使您的代码正常工作。