web服务—使用由windowsauthentication保护的iis上托管的web服务时出现java问题

2ul0zpep  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(252)

应用程序描述
行为不正常的应用程序是一个android应用程序,它允许用户从手机上拍照,然后上传到sharepointcms。
详细问题描述
在java应用程序上,我将一个post-http请求(包含base64格式的图像字节)发送到iis上的soap-webservice。此Web服务使用windows身份验证进行保护。我已经将java程序配置为在发出请求时发送凭据。当我检查http响应代码时,它是http 401未经授权的。我无法检查回复内容。
注意事项
java调试器没有像应该的那样进入authenticator类的getpasswordauthentication方法。
应用程序规格
编程语言:java
应用类型:android应用
代码位置:异步任务内部
端点类型:soap webservice
端点身份验证:windows身份验证
相关代码
连接设置

URL url = new URL(ENDPOINT_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// Enable POST
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "text/xml");

始终发送存储凭据的身份验证程序设置

Authenticator.setDefault(new NTLMAuthenticator());

ntlmauthenticator类

public class NTLMAuthenticator extends Authenticator {
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("USER_NAME", "PASSWORD".toCharArray());
    }
}

请求发布内容设置

try (OutputStream outputStream = httpURLConnection.getOutputStream()) {
    String baseRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><Upload xmlns=\"http://schemas.microsoft.com/sharepoint/soap/ois/\"><strListName>%s</strListName><strFolder /><bytes>%s</bytes><fileName>%s</fileName><fOverWriteIfExist>true</fOverWriteIfExist></Upload></soap:Body></soap:Envelope>";
    File f = new File(PATH_TO_FILE);
    byte[] fileBytes = FileUtils.readFileToByteArray(f);
    String fileBase64 = android.util.Base64.encodeToString(fileBytes, 0);
    outputStream.write(String.format(baseRequest, LIST_NAME, fileBase64, FILE_NAME).getBytes());
    outputStream.flush();
} catch (Exception e) {
    e.printStackTrace();
}

响应读取

int response_code = httpURLConnection.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
    //**response_code IS HTTP_UNAUTHORIZED**
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
        //**Unreachable code**
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            // Read response line by line
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题