简体   繁体   English

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

[英]Unable to send SOAP post request to bsemfstardemo.bseindia.com in java

I have tried to POST data to a SOAP web service provided by bsestarmfdemo.bseindia.com. 我试图将数据发布到bsestarmfdemo.bseindia.com提供的SOAP Web服务中。 But I have got the following error while trying to execute the getPassword service.The error is as follows:- 但是我在尝试执行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)

This is my java class that I used to access the getPassword service provided by the bsestarmf.bseindia.com :- 这是我用来访问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;
    }
}

Please let me know where I made the mistake. 请让我知道我在哪里弄错了。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance! 提前致谢!

It looks like the server is not SOAP 1.2 compliant. 看起来服务器不符合SOAP 1.2。 SOAP 1.2 expects the ContentType application/soap+xml but your server is sending text/xml which is used by SOAP 1.1. SOAP 1.2期望使用ContentType application/soap+xml但是您的服务器正在发送SOAP 1.1使用的text/xml

You could try using 您可以尝试使用

MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

to create a SOAP 1.1 message. 创建SOAP 1.1消息。

I have changed one of the methods as follows and it worked for me. 我更改了以下方法之一,它对我有用。 Thanks a lot! 非常感谢!

private static SOAPMessage createSOAPRequest(String soapAction) throws SOAPException, IOException {
        //MessageFactory messageFactory = MessageFactory.newInstance();
        MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        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");

        SOAPHeader header = soapMessage.getSOAPHeader();
        SOAPHeaderElement actionElement = header.addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "Action"));
        actionElement.setMustUnderstand(true);
        actionElement.addTextNode(soapActionUrl);

        SOAPHeaderElement toElement = header.addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "To"));
        toElement.setMustUnderstand(true);
        toElement.addTextNode(soapEndPointUrl);

        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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM