Guest User

Untitled

a guest
Dec 5th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public class AuthCallout {
  2.  
  3. public void basicAuthCallout(){
  4. HttpRequest req = new HttpRequest();
  5. req.setEndpoint('http://www.yahoo.com');
  6. req.setMethod('GET');
  7.  
  8. // Specify the required user name and password to access the endpoint
  9.  
  10. // As well as the header and header information
  11.  
  12.  
  13. String username = 'myname';
  14. String password = 'mypwd';
  15.  
  16. Blob headerValue = Blob.valueOf(username + ':' + password);
  17. String authorizationHeader = 'BASIC ' +
  18. EncodingUtil.base64Encode(headerValue);
  19. req.setHeader('Authorization', authorizationHeader);
  20.  
  21. // Create a new http object to send the request object
  22.  
  23. // A response object is generated as a result of the request
  24.  
  25.  
  26. Http http = new Http();
  27. HTTPResponse res = http.send(req);
  28. System.debug(res.getBody());
  29. }
  30. }
  31.  
  32. public static void sendNotification(String name, String city) {
  33.  
  34. HttpRequest req = new HttpRequest();
  35. HttpResponse res = new HttpResponse();
  36. Http http = new Http();
  37.  
  38. req.setEndpoint('http://my-end-point.com/newCustomer');
  39. req.setMethod('POST');
  40. req.setBody('name='+EncodingUtil.urlEncode(name, 'UTF-8')+'&city='+EncodingUtil.urlEncode(city, 'UTF-8'));
  41. req.setCompressed(true); // otherwise we hit a limit of 32000
  42.  
  43. try {
  44. res = http.send(req);
  45. } catch(System.CalloutException e) {
  46. System.debug('Callout error: '+ e);
  47. System.debug(res.toString());
  48. }
  49.  
  50. }
  51.  
  52. // run WebServiceCallout.testMe(); from Execute Anonymous to test
  53. public static testMethod void testMe() {
  54. WebServiceCallout.sendNotification('My Test Customer','My City');
  55. }
  56.  
  57. HttpRequest req = new HttpRequest();
  58. HttpResponse res = new HttpResponse();
  59. Http http = new Http();
  60.  
  61. req.setEndpoint('https://www.mysite.com/myendpoint');
  62. req.setMethod('POST');
  63.  
  64. //these parts of the POST you may want to customize
  65. req.setCompressed(false);
  66. req.setBody('key1=value1&key2=value2');
  67. req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  68.  
  69. try {
  70. res = http.send(req);
  71. } catch(System.CalloutException e) {
  72. System.debug('Callout error: '+ e);
  73. }
  74. System.debug(res.getBody());
  75.  
  76. HttpRequest req = new HttpRequest();
  77. HttpResponse res = new HttpResponse();
  78. Http http = new Http();
  79.  
  80. req.setEndpoint('callout:My_Named_Credential/some_path');
  81. req.setMethod('POST');
  82. req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  83. req.setBody('key1=value1&key2=value2');
  84.  
  85. try {
  86. res = http.send(req);
  87. if (res.getStatusCode() == 200) {
  88. System.debug('Success!');
  89. } else {
  90. System.debug('HTTP error: ' + res.getStatusCode());
  91. }
  92. System.debug(res.getBody());
  93. } catch(System.CalloutException e) {
  94. System.debug('Callout error: '+ e);
  95. }
Add Comment
Please, Sign In to add comment