Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java摘要认证POST XML_Java_Http_Xmlhttprequest_Digest Authentication - Fatal编程技术网

Java摘要认证POST XML

Java摘要认证POST XML,java,http,xmlhttprequest,digest-authentication,Java,Http,Xmlhttprequest,Digest Authentication,我需要一些帮助才能工作。我正在使用Apache4.1库。当我尝试登录时,我得到了 线程“main”javax.net.ssl.SSLPeerUnverifiedException中的异常:对等未经身份验证 我正在尝试登录Asterisk SwitchVox Dev Extend API,您只需发送一个xml帖子,它就会返回信息。我当然有正确的用户名/密码,我在一个PERL脚本上得到了它,但我在JAVA中无法得到它 这是我的密码 public class Main { public static

我需要一些帮助才能工作。我正在使用Apache4.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();
    }
}
公共类主{
公共静态void main(字符串[]args)引发异常{
HttpHost targetHost=新的HttpHost(“192.168.143.253”,443,“https”);
DefaultHttpClient httpclient=新的DefaultHttpClient();
试一试{
httpclient.getCredentialsProvider().setCredentials(
新的AuthScope(“192.168.143.253”,targetHost.getPort()),
新用户名密码凭证(“用户名”、“我的密码”);
//创建AuthCache实例
AuthCache AuthCache=new BasicAuthCache();
//生成摘要方案对象,初始化它并将其添加到本地身份验证缓存
DigestScheme digestAuth=新的DigestScheme();
put(targetHost,digestAuth);
//将AuthCache添加到执行上下文
BasicHttpContext localcontext=新的BasicHttpContext();
setAttribute(ClientContext.AUTH\u缓存,authCache);
HttpGet HttpGet=新的HttpGet(“https://192.168.143.253/xml/");
System.out.println(“正在执行请求:+httpget.getRequestLine());
System.out.println(“目标:+targetHost”);
对于(int i=0;i<3;i++){
HttpResponse response=httpclient.execute(targetHost、httpget、localcontext);
HttpEntity=response.getEntity();
System.out.println(“--------------------------------------------------------”;
System.out.println(response.getStatusLine());
如果(实体!=null){
System.out.println(“响应内容长度:+entity.getContentLength());
}
EntityUtils.consume(实体);
}
}最后{
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpclient.getConnectionManager().shutdown();
}
}

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

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());

}
publicstaticvoidmain(字符串参数[]){
最终字符串username=“user”;
最终字符串password=“password”;
setDefault(新验证器(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
PasswordAuthentication pa=新的PasswordAuthentication(用户名,password.tocharray());
//System.out.println(pa.getUserName()+“:”+新字符串(pa.getPassword());
返回pa;
}
});
BufferedReader in=null;
StringBuffer sb=新的StringBuffer();
试一试{
//URL=新URL(strURL);
HttpsURLConnection连接=(HttpsURLConnection)新URL(“https://secureHost/”).openConnection();
setDefaultHostnameVerifier(新的定制HostNameVerifier());
setHostnameVerifier(新的定制HostNameVerifier());
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“文本/xml”);
PrintWriter out=新的PrintWriter(connection.getOutputStream());
字符串requestString=“”;
out.println(请求字符串);
out.close();
in=新的BufferedReader(新的InputStreamReader(连接
.getInputStream());
弦线;
而((line=in.readLine())!=null){
sb.append(行)。append(“\n”);
}
}catch(java.net.ProtocolException e){
sb.append(“用户或密码错误!”);
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
if(in!=null){
in.close();
}
}捕获(例外e){
System.out.println(“例外”);
}
}
System.out.println(“数据为:+sb.toString());
}

}

您没有使用
DigestScheme digestAuth=new DigestScheme()在你的答案中。这是如何与摘要安全性一起工作的(我知道代码是如何工作的,我想知道它的安全性)