Java Beispielcode


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class SimpleRequest {

    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.adresslabor.de/v1/de/check");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");

            // Products to use
            String product = URLEncoder.encode("EM,SC,FK,PB,RB", "UTF8");
            // Api key and apcid
            String apicid = Long.toString(1001);
            String apikey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

            // Urlencoded Fields
            String firstname = URLEncoder.encode("Donald", "UTF8");
            String lastname = URLEncoder.encode("Duck", "UTF8");
            String street = URLEncoder.encode("grabenstr.", "UTF8");
            String hno = URLEncoder.encode("4", "UTF8");
            String zip = URLEncoder.encode("12345", "UTF8");
            String city = URLEncoder.encode("Entenhausen", "UTF8");
            String country = URLEncoder.encode("DE", "UTF8");
            String phone = URLEncoder.encode("06021/999999", "UTF8");
            String email = URLEncoder.encode("donald@duck.com", "UTF8");

            StringBuilder contentToPost = new StringBuilder();
            contentToPost.append("product=").append(product).append('&');
            contentToPost.append("firstname=").append(firstname).append('&');
            contentToPost.append("lastname=").append(lastname).append('&');
            contentToPost.append("street=").append(street).append('&');
            contentToPost.append("hno=").append(hno).append('&');
            contentToPost.append("zip=").append(zip).append('&');
            contentToPost.append("city=").append(city).append('&');
            contentToPost.append("country=").append(country).append('&');
            contentToPost.append("phone=").append(phone).append('&');
            contentToPost.append("email=").append(email).append('&');
            contentToPost.append("apicid=").append(apicid).append('&');
            contentToPost.append("apikey=").append(apikey);

            // Send Data
            con.setDoOutput(true);
            OutputStream stream = con.getOutputStream();
            stream.write(contentToPost.toString().getBytes());
            stream.close();

            // Read Result
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String str = br.readLine();
            while (str != null) {
                sb.append(str);
                str = br.readLine();
            }
            br.close();

            // Print the whole json-result
            System.out.println(sb.toString());

            // use a json-library to parse the result, eg.
            // see http://json.org/

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}