无法使用Java将SOAP发布请求发送到bsemfstardemo.bseindia.com

时间:2019-02-13 10:46:05

标签: java soap

我试图将数据发布到bsestarmfdemo.bseindia.com提供的SOAP Web服务中。但是尝试执行getPassword服务时遇到以下错误。错误如下:-

Feb 13, 2019 3:56:14 PM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (415Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.callSoapWebService(SoapMessageTest.java:63)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.main(SoapMessageTest.java:30)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (415Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
    ... 2 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (415Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.callSoapWebService(SoapMessageTest.java:63)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.main(SoapMessageTest.java:30)

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (415Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.callSoapWebService(SoapMessageTest.java:63)
    at com.bsoft.wv.bsestarmf.test.SoapMessageTest.main(SoapMessageTest.java:30)

这是我用来访问bsestarmf.bseindia.com提供的getPassword服务的Java类:-

package com.bsoft.wv.bsestarmf.test;

import java.io.IOException;
import java.sql.Savepoint;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

/**
 * @author bosco
 *
 */
public class SoapMessageTest {

    public static void main(String[] args) {

        String soapEndPointUrl = "http://bsestarmfdemo.bseindia.com/MFUploadService/MFUploadService.svc/Basic";
        String soapActionUrl = "http://bsestarmfdemo.bseindia.com/2016/01/IMFUploadService/getPassword";
        callSoapWebService(soapEndPointUrl,soapActionUrl);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessgae) throws SOAPException {
        SOAPPart soapPart = soapMessgae.getSOAPPart();

        //String myNameSpace = "http://www.w3.org/2003/05/soap-envelope";
        String myNameSpace  = "ns";
        String myNameSpaceURI = "http://bsestarmfdemo.bseindia.com/2016/01/";

        SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
        soapEnvelope.addNamespaceDeclaration(myNameSpace, myNameSpaceURI);

        SOAPBody soapBody = soapEnvelope.getBody();
        SOAPElement soapBodyElement = soapBody.addChildElement("getPassword",myNameSpace);
        SOAPElement soapBodyElement2 = soapBodyElement.addChildElement("UserId",myNameSpace);
        SOAPElement soapBodyElement3 = soapBodyElement.addChildElement("MemberId", myNameSpace);
        SOAPElement soapBodyElement4 = soapBodyElement.addChildElement("Password", myNameSpace);
        SOAPElement soapBodyElement5 = soapBodyElement.addChildElement("PassKey", myNameSpace);
        soapBodyElement2.addTextNode("1236548");
        soapBodyElement3.addTextNode("12365");
        soapBodyElement4.addTextNode("Tester@1");
        soapBodyElement5.addTextNode("test@gmail.com");
    }

    private static void callSoapWebService(String soapEndPointUrl, String soapAction) {

        try {
            //SOAP Connection creation 
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            //Send SOAP message to SOAP server
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndPointUrl);

            //Print SOAP response...
            System.out.println("SOAP Response");
            soapResponse.writeTo(System.out);
            System.out.println();
            soapConnection.close();
        }  catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws SOAPException, IOException {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        createSoapEnvelope(soapMessage);
        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);
        headers.addHeader("Content-Type", "application/soap+xml; charset=utf-8");
        headers.addHeader("Encoding", "UTF-8");
        soapMessage.saveChanges();
        System.out.println("Soap Body: "+soapMessage.getSOAPBody());
        System.out.println("Request SOAP Message");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }
}

请让我知道我在哪里弄错了。任何帮助,将不胜感激。提前致谢!

2 个答案:

答案 0 :(得分:1)

服务器似乎不符合SOAP 1.2。 SOAP 1.2需要ContentType application/soap+xml,但是您的服务器正在发送SOAP 1.1使用的text/xml

您可以尝试使用

MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

创建SOAP 1.1消息。

答案 1 :(得分:0)

我更改了以下一种方法,它对我有用。非常感谢!

> npm -v
6.7.0