import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.http.HttpHost; import org.apache.http.StatusLine; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; public class Ff2 { private static final String WS_URI = "/ws410/rest"; //heavy threadsafe object. should be sigleton to speedup public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); public static void main(String[] args) throws Exception { String scheme = "http"; String host = "localhost"; int port = 9001; String login = "admin"; String password = "nimda"; getDataFromWs(scheme, host, port, login, password, "/catalogs/blackberryProductCatalog/catalogversions/Online/products/SPA-60801-001", "{\"@uri\": \"http://localhost:9001/ws410/rest/catalogs/blackberryProductCatalog/catalogversions/Online/products/SPA-60801-001\",\n" + "\"name\": \"fuu223455555\"}" ); } private static void getDataFromWs(String scheme, String host, int port, String login, String password, String uri, String body) throws IOException { HttpHost target = new HttpHost(host, port, scheme); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(login, password)); try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) { AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); HttpPut httpPut = new HttpPut(WS_URI + uri); httpPut.addHeader("Accept", "application/json"); httpPut.addHeader("Content-Type", "application/json"); httpPut.setEntity(new StringEntity(body)); try (CloseableHttpResponse response = httpclient.execute(target, httpPut, localContext)) { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() != 200) { throw new IOException("WS invocation error: " + statusLine.toString()); } } } } }