如何使用Java调用Shopware的API清除缓存

时间:2019-09-12 11:55:21

标签: java rest api httpclient apache-commons-httpclient

最近8个月来我一直在使用Shopware 5.5和5.6,已将其安装在ubuntu服务器上以访问我的商店,我需要服务器登录凭据。根据Shopware文档,API调用需要Shopware用户名和API密钥。我的问题是如何使用服务器登录凭据以及Shopware的用户名和API密钥来调用Shopware的API。

String finalURL = "https://www.myserver.com/shopware/api/caches/";
                   //API URL to clear cache
            URL url = new URL( finalURL );
            Base64 b = new Base64();
            String encoding = b.encodeAsString( ("SERVER_USERNAME" + ":" + "SERVER_PASSWORD").getBytes() );
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod( "GET" );
            connection.setDoOutput( true );
            connection.setRequestProperty( "Authorization", "Basic " + encoding );
            connection.setUseCaches( false );

            //Checking connection response
            if (connection.getResponseCode() != 200) {
                System.out.println( "Status: " + connection.getResponseMessage() );

            } else {
                System.out.println("Connection established!<br>" );
            }
            long start = System.currentTimeMillis();
            System.out.print( "\nDownloading data from IT Scope," );
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

                br = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );`enter code here`
                stream = br.lines();

                stream.forEach(System.out::println);
            }

我正在尝试这种方式,但是没有成功。

0 个答案:

没有答案