Rest方法在静态时抛出异常

时间:2015-01-19 17:06:30

标签: java rest httpclient

我正在尝试RestApi调用,下面的代码我之前没有使用静态方法,现在它是一个静态方法,它在下面抛出这个异常:当它在同一个类中时,它的工作原理

  

原因:java.io.EOFException:SSL对等关闭不正确   DetailMessage:握手期间远程主机关闭连接

public static void getMethod(String pass,String query,String url)throws Exception{

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
           AuthScope.ANY,
            new UsernamePasswordCredentials("fzs", pass));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).setProxy(new HttpHost("proxyName",80))
            .build();

    try {

        String encode = url + (URLEncoder.encode(query,"UTF-8"));

        HttpUriRequest httpget = new HttpGet(encode);


        System.out.println("Executings request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
       try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            String message = EntityUtils.toString(entity);

            EntityUtils.consume(entity);
            System.out.println(message);

        } finally {
            response.close();
        }
    }
    catch (Exception e)
    {
         System.out.println(e.getMessage());
    }
    finally {
        httpclient.close();
    }
}

工作代码:

@Test   
    public void RestAPI() throws Exception {


        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
               AuthScope.ANY,
                new UsernamePasswordCredentials("fzs", pass));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).setProxy(new HttpHost("proxyName",80))
                .build();

        try {

            String query = "active=true^state!=3^u_entity=u_case^GOTOnumber=CASE0014474";
            String url = "https://service-now.com/api/now/table/u_case?sysparm_query=";



            String encode = url + (URLEncoder.encode(query,"UTF-8"));

            HttpUriRequest httpget = new HttpGet(encode);


            System.out.println("Executings request " + httpget.getRequestLine());
       //     ResponseHandler<String> responseHandler=new BasicResponseHandler();
        //    String responseBody = httpclient.execute(httpget, responseHandler);
            CloseableHttpResponse response = httpclient.execute(httpget);
           try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
               // System.out.println(responseBody);

            } finally {
                response.close();
            }
        }
        catch (Exception e)
        {
             System.out.println(e.getMessage());
        }
        finally {
            httpclient.close();
        }
    }

0 个答案:

没有答案