Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Date;
  5.  
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.SerializableEntity;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.impl.client.HttpClients;
  11. import pl.benhauer.salesmanago.api.ContactInsertRequest;
  12. import pl.benhauer.salesmanago.api.ContactInsertResponse;
  13. import pl.benhauer.salesmanago.api.model.SmAddress;
  14. import pl.benhauer.salesmanago.api.model.SmContact;
  15. import pl.benhauer.salesmanago.api.samples.CryptoTools;
  16.  
  17.  
  18.  
  19.  
  20. public class Przyklad
  21. {
  22.     final public static String  endpoint = "http://app3.salesmanago.pl/api";
  23.     final public static String clientId = "eeaztded5f45hfn0";
  24.     final public static String apiSecret = "dfhic8sohbeie1smkn5kwh7a692vpbvo";
  25.     final public static String apiKey = "71a3m9dlu2cqznc3hs64gt9tj927ymw6";
  26.    
  27.    
  28.     public static void main(String[] args)
  29.     {
  30.         ContactInsertRequest contactInsertRequest = new ContactInsertRequest(clientId, apiKey, new Date(),
  31.                 CryptoTools.sha1(apiKey + clientId + apiKey));
  32.        
  33.         contactInsertRequest.setOwner("info@cosurfing.pl");
  34.        
  35.         SmContact contact = new SmContact();
  36.         contact.setEmail("test_mail_karol_m@wp.pl");
  37.         contact.setName("Karol");
  38.         contact.setPhone("222-222-222");
  39.         SmAddress address = new SmAddress();
  40.         address.setCity("Kielce");
  41.         address.setCountry("PL");
  42.         contact.setAddress(address);
  43.        
  44.         ContactInsertResponse contactInsertResponse = null;
  45.         HttpResponse httpResponse = null;
  46.        
  47.         CloseableHttpClient httpclinet = HttpClients.createDefault();
  48.        
  49.         HttpPost post = new HttpPost(endpoint + "/contact/insert");
  50.         post.setHeader("Accept", "application/json, application/json");
  51.         post.setHeader("Content-Type", "application/json;charset=UTF-8");
  52.         try
  53.         {
  54.             post.setEntity(new SerializableEntity(contactInsertRequest, false));
  55.            
  56.         } catch (IOException e1) { System.out.println("Nie mozna dodac contactInsertRequest do POST"); }
  57.        
  58.         try
  59.         {
  60.             HttpResponse htresponse = httpclinet.execute(post);
  61.             BufferedReader br = new BufferedReader(
  62.                     new InputStreamReader((htresponse.getEntity().getContent())));
  63.             System.out.println("Output from Server .... \n");
  64.             StringBuffer result = new StringBuffer();
  65.             String output = "";
  66.             while ((output = br.readLine()) != null)
  67.                 result.append(output + '\n');
  68.            
  69.             System.out.println(result.toString());
  70.            
  71.            
  72.         } catch (IOException e) { System.out.println("Nie mozna wykonac POST"); }
  73.        
  74.        
  75.         try
  76.         {
  77.             httpclinet.close();
  78.         } catch(IOException e) { System.out.println("Nie mozna zamknac HttpClient");}
  79.        
  80.     }
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement