Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package co.tuzza.clicksend4j.http; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.util.ArrayList; import java.util.List; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.message.BasicNameValuePair; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * * @author dylan */ public class HttpClientUtilsTest { public HttpClientUtilsTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } @After public void tearDown() { } /** * Test of getHttpClient method, of class HttpClientUtils. */ @Test public void testGetHttpClient() throws ProtocolException, UnsupportedEncodingException, IOException { System.out.println("getHttpClient"); HttpClientUtils instance = new HttpClientUtils(10000, 10000); HttpClient expResult = null; HttpClient httpClient = instance.getHttpClient(); // https://api.clicksend.com/rest/v2/send.json?method=rest&message=This%20is%20the%20message&to=%2B8611111111111%2C%2B61411111111%20 String loginPassword = "tuzzmaniandevil:5C148EA8-D97B-B5F8-1A3F-1BE0658F4DF5"; String auth = Base64.encodeBase64String(loginPassword.getBytes()); HttpUriRequest method; List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("method", "rest")); params.add(new BasicNameValuePair("to", "+61411111111")); params.add(new BasicNameValuePair("message", "This is a test message")); HttpPost httpPost = new HttpPost("https://api.clicksend.com/rest/v2/send.json"); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpPost.setHeader("Authorization", "Basic " + auth); method = httpPost; String url = "https://api.clicksend.com/rest/v2/send.json" + "?" + URLEncodedUtils.format(params, "utf-8"); HttpResponse httpResponse = httpClient.execute(method); int status = httpResponse.getStatusLine().getStatusCode(); String response = new BasicResponseHandler().handleResponse(httpResponse); } }