Guest User

Untitled

a guest
Jan 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import org.apache.http.auth.AuthScope;
  2. import org.apache.http.auth.UsernamePasswordCredentials;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.impl.client.BasicCredentialsProvider;
  5. import org.apache.http.impl.client.HttpClientBuilder;
  6. import org.springframework.http.client.ClientHttpRequestFactory;
  7. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  8. import org.springframework.web.client.RestTemplate;
  9.  
  10. ...
  11.  
  12. BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  13. credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("login", "password"));
  14. HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
  15. ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  16.  
  17. RestTemplate restTemplate = new RestTemplate(requestFactory);
  18. String url = "http://host:8080/path/";
  19. String postBody = getPostInput("filename");
  20. jsonString = restTemplate.postForObject(path, postBody, String.class);
  21.  
  22. HttpHeaders headers = new HttpHeaders();
  23. headers.setContentType(MediaType.APPLICATION_JSON);
  24.  
  25. HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
  26. restTemplate.put(uRL, entity);
  27.  
  28. RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson);
  29. restTemplate.exchange(requestEntity, responseClass);
  30.  
  31. HttpHeaders headers = new HttpHeaders();
  32. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  33.  
  34. // if you need to pass form parameters in request with headers.
  35. MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
  36. map.add("username", userName);
  37. map.add("password", password);
  38.  
  39. HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
  40. ResponseEntity<TokenVO> responses = restTemplate.postForEntity(URL, request, responseClass);
Add Comment
Please, Sign In to add comment