Advertisement
Guest User

Untitled

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