2017-12-24 71 views
0

이 질문이 중복되면 StackOverflow에서 몇 가지 해결책을 시도했지만 여전히 SOAP 요청을 보내는 데 문제가 있습니다.SOAP UI에서 자바를 사용하여 SOAP 사용자 인터페이스와 같은 SOAP 요청을 보내는 방법

나의 현재 상황은 내 고객 웹 서비스에 SOAP 요청을 보내야하는데, SOAP 사용자 인터페이스에서 성공적으로 수행 한 사용자 이름, 암호, 인증 유형 : 선점과 함께.

enter image description here

가 지금은 자바에서이 작업을 수행 할 사진을 참조하십시오, 이미 암호를 설정하지 않고 SOAP 봉투를 생성 한 응답은 정확히 어떤 인증을 사용하지 SOAP의 UI와 동일하다 java 코드가 작동하도록 인증을 추가하기 만하면된다는 것을 의미합니다. 여기에 내 현재의 Java 코드 :

callSoapWebService(soapEndpointUrl, soapAction); 

private static void callSoapWebService(String soapEndpointUrl, String soapAction) { 
    try { 
     // Create SOAP Connection 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

     // Send SOAP Message to SOAP Server 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl); 

     // Print the SOAP Response 
     System.out.println("Response SOAP Message:"); 
     soapResponse.writeTo(System.out); 
     System.out.println(); 

     soapConnection.close(); 
    } catch (Exception e) { 
     System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n"); 
     e.printStackTrace(); 
    } 
} 

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception { 
    MessageFactory messageFactory = MessageFactory.newInstance(); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 

    createSoapEnvelope(soapMessage); 

    MimeHeaders headers = soapMessage.getMimeHeaders(); 
    headers.addHeader("SOAPAction", soapAction); 

    soapMessage.saveChanges(); 

    return soapMessage; 
} 

내부 createSoapEnvelope() SOAP 요청 본문을 생성하는 코드는, 그래서 내가 헤더에 뭔가를해야 할 것 같아요. 누구나 SOAP UI에서했던 것처럼 인증을 얻을 수 있습니까?

답변

2

당신은 기본 형식으로 사용자 이름과 암호를 보낼 필요가 :

String userAndPassword = String.format("%s:%s",username, password); 

String basicAuth = new sun.misc.BASE64Encoder().encode(userAndPassword.getBytes()); 
MimeHeaders mimeHeaders = message.getMimeHeaders(); 
mimeHeaders.addHeader("Authorization", "Basic " + basicAuth); 

자바 8 사용 Base64.getEncoder().encodeToString() 대신 new sun.misc.BASE64Encoder()를 사용하는 경우.