Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.HttpClient;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.springframework.http.client.ClientHttpRequestFactory;
- import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
- import org.springframework.web.client.RestTemplate;
- ...
- BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("login", "password"));
- HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
- ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
- RestTemplate restTemplate = new RestTemplate(requestFactory);
- String url = "http://host:8080/path/";
- String postBody = getPostInput("filename");
- jsonString = restTemplate.postForObject(path, postBody, String.class);
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_JSON);
- HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
- restTemplate.put(uRL, entity);
- RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson);
- restTemplate.exchange(requestEntity, responseClass);
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- // if you need to pass form parameters in request with headers.
- MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
- map.add("username", userName);
- map.add("password", password);
- HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
- ResponseEntity<TokenVO> responses = restTemplate.postForEntity(URL, request, responseClass);
Add Comment
Please, Sign In to add comment