Java 摘要式身份验证 POST XML

发布于 2024-10-28 08:15:57 字数 2181 浏览 0 评论 0原文

我需要一些帮助才能使摘要身份验证正常工作。我正在使用 apache 4.1 库。当我尝试登录时,我得到了。

线程“main”中的异常 javax.net.ssl.SSLPeerUnverifiedException: 对等点未经过身份验证

我正在尝试登录到 Asterisk SwitchVox Dev Extend API,您只需发送 xml 帖子即可返回信息。我当然有正确的用户名/密码,并且我在 PERL 脚本上得到了这个工作,但我只是无法在 JAVA 中得到它。

这是我的代码

public class Main {

public static void main(String[] args) throws Exception {

    HttpHost targetHost = new HttpHost("192.168.143.253", 443, "https");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("192.168.143.253", targetHost.getPort()),
                new UsernamePasswordCredentials("username", "mypassword"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache

        DigestScheme digestAuth = new DigestScheme();

        authCache.put(targetHost, digestAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("https://192.168.143.253/xml/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

}

I am in need of some help getting DIGEST Authentication to work. I am using the apache 4.1 library. When i try to login i get.

Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

I am trying to login to a the Asterisk SwitchVox Dev Extend API, which you simply send an xml post and it give you back information. I certainly have the correct username/password and i got this working on a PERL script but i just cant get it in JAVA.

Here is my code

public class Main {

public static void main(String[] args) throws Exception {

    HttpHost targetHost = new HttpHost("192.168.143.253", 443, "https");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("192.168.143.253", targetHost.getPort()),
                new UsernamePasswordCredentials("username", "mypassword"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache

        DigestScheme digestAuth = new DigestScheme();

        authCache.put(targetHost, digestAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("https://192.168.143.253/xml/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-11-04 08:15:57

我终于找到了我的问题的答案。

public static void main(String args[]) {

    final String username = "user";
    final String password = "password";



    Authenticator.setDefault(new Authenticator() {
        @Override
          protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
                //System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
                return pa;
            }
          });
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();

    try {
        //URL url = new URL(strURL);

        HttpsURLConnection connection = (HttpsURLConnection) new URL("https://secureHost/").openConnection();
                    connection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
                    connection.setHostnameVerifier(new CustomizedHostnameVerifier());
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type","text/xml");
                    PrintWriter out = new PrintWriter(connection.getOutputStream());
                    String requestString = "<request method=\"switchvox.currentCalls.getList\"></request>";

                    out.println(requestString);
                    out.close();

        in = new BufferedReader(new InputStreamReader(connection
                .getInputStream()));

        String line;

        while ((line = in.readLine()) != null) {
            sb.append(line).append("\n");
        }
    } catch (java.net.ProtocolException e) {
        sb.append("User Or Password is wrong!");
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            System.out.println("Exception");
        }
    }

    System.out.println("The Data is: " + sb.toString());

}

}

I finally found the answer to my question.

public static void main(String args[]) {

    final String username = "user";
    final String password = "password";



    Authenticator.setDefault(new Authenticator() {
        @Override
          protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
                //System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
                return pa;
            }
          });
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();

    try {
        //URL url = new URL(strURL);

        HttpsURLConnection connection = (HttpsURLConnection) new URL("https://secureHost/").openConnection();
                    connection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
                    connection.setHostnameVerifier(new CustomizedHostnameVerifier());
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type","text/xml");
                    PrintWriter out = new PrintWriter(connection.getOutputStream());
                    String requestString = "<request method=\"switchvox.currentCalls.getList\"></request>";

                    out.println(requestString);
                    out.close();

        in = new BufferedReader(new InputStreamReader(connection
                .getInputStream()));

        String line;

        while ((line = in.readLine()) != null) {
            sb.append(line).append("\n");
        }
    } catch (java.net.ProtocolException e) {
        sb.append("User Or Password is wrong!");
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            System.out.println("Exception");
        }
    }

    System.out.println("The Data is: " + sb.toString());

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文