Accessing SOS with Basic Authentication - Java

Posted by Linda Derezinski Mon, 23 Oct 2006 13:06:00 GMT

Basic authentication in Java example.

This uses JDK 5.0 and Apache jakarta commons libraries.

JDK download: http://java.sun.com/javase/downloads/index.jsp

Apache download: http://jakarta.apache.org/commons/index.html

Example XML to pass to the SOS is found http://eo1.geobliki.com/sosdemo

testPost(XML, "user", "password", false, "http://eo1.geobliki.com/sos");

If you are having issues with firewalls try changing the preempt to true to see if will work. This always sends the username/password with the request.

public static void testPost(String xml, String userName, String password, boolean preempt, String URL){
    HttpClient client = new HttpClient();
    HttpState state = client.getState();
    HttpClientParams params = client.getParams();
    params.setAuthenticationPreemptive(preempt);
    Credentials credentials = new UsernamePasswordCredentials(userName, password);
    state.setCredentials(null, null, credentials);
    PostMethod method = new PostMethod(URL);
    method.setRequestBody(xml);
    try {
        client.executeMethod(method);       
        Header[] headers = method.getResponseHeaders();
        for (Header header:headers){
            System.out.println(header.getName() + ":"+header.getValue());
        }
        String response = method.getResponseBodyAsString();
        System.out.println(response);
        method.releaseConnection();
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}
Comments

Leave a comment

Comments