【问题标题】:Getting javax.xml.soap.SOAPException: Content-Type not specified error on Oracle weblogic server while calling web service through Java获取 javax.xml.soap.SOAPException:通过 Java 调用 Web 服务时在 Oracle WebLogic 服务器上未指定 Content-Type 错误
【发布时间】:2018-11-26 11:53:12
【问题描述】:

我使用 Jdeveloper 工具编写了一个 java 代码来调用外部 Web 服务。当我从 Jdeveloper 运行我的代码时,它工作正常。但我在 Oracle weblogic 10.3.6 服务器上部署我的代码并尝试运行此代码然后它给了我以下错误 -

javax.xml.soap.SOAPException:未指定内容类型

这是我的代码 -

    public static String callTestService(String soapRequestXml, String url)  {


        try{

        final boolean isHttps = url.toLowerCase().startsWith("https");
        HttpsURLConnection httpsConnection = null;
                // Open HTTPS connection

                // Create a trust manager that does not validate certificate chains
                       TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                               public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                   return null;
                               }
                               public void checkClientTrusted(X509Certificate[] certs, String authType) {
                               }
                               public void checkServerTrusted(X509Certificate[] certs, String authType) {
                               }
                           }
                       };

                if (isHttps) {
                    // Create SSL context and trust all certificates
                    SSLContext sslContext = SSLContext.getInstance("SSL");
                    //TrustManager[] trustAll= new TrustManager[] {new TrustAllCertificates()};
                   sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
                    // Set trust all certificates context to HttpsURLConnection
                    HttpsURLConnection
                            .setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                    // Open HTTPS connection
                  //  URL url1 = new URL(url);
                  URL url1 = new URL(null, url, new sun.net.www.protocol.https.Handler());
                    httpsConnection = (HttpsURLConnection) url1.openConnection();
                    // Trust all hosts
             //       httpsConnection.setHostnameVerifier(new TrustAllHosts());
                    // Connect

                    //Use below (2 lines) if Host name verification needs to turned off
                    MyHostnameVerifier HostVerifier = new MyHostnameVerifier();
                    httpsConnection.setHostnameVerifier(HostVerifier);
                    httpsConnection.connect();
                }
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        System.out.println("create connection");
     // SOAPMessage soapRequest = MessageFactory.newInstance().createMessage(new MimeHeaders(),
       //        new ByteArrayInputStream(soapRequestXml.getBytes()));

        SOAPMessage soapRequest = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(new MimeHeaders() ,
                 new ByteArrayInputStream(soapRequestXml.getBytes(Charset.forName("UTF-8"))));
           // SOAPMessage s = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
           // SOAPPart soapPart = soapRequest.getSOAPPart();

                // SOAP Envelope
               // SOAPEnvelope envelope = soapPart.getEnvelope();
               // envelope.addNamespaceDeclaration("Dokumentai", "http://vmi.lt/ais/xsd/daa");

    //  MessageFactory messageFactory = MessageFactory.newInstance();
        //SOAPMessage soapRequest = messageFactory.createMessage(new MimeHeaders(),soapRequestXml);
     // MessageFactory messageFactory = MessageFactory.newInstance();
      //  SOAPMessage soapRequest = messageFactory.createMessage();
    //  SOAPPart soapPart = soapRequest.getSOAPPart();
    //   SOAPEnvelope envelope = soapPart.getEnvelope();
     //   SOAPBody soapBody = envelope.getBody();
      //soapBody.setTextContent(soapRequestXml);
     // soapRequest.setContentDescription(soapRequestXml);

   // SOAPPart soapPart = soapRequest.getSOAPPart();
    //SOAPEnvelope envelope = soapPart.getEnvelope();

     // String serverURI = "myhost";


   //  envelope.addNamespaceDeclaration("xmlns",
     //  "http://vmi.lt/ais/xsd/daa");






     // SOAP Envelope


   //  SOAPEnvelope envelope = soapPart.setContent(soapRequestXml);
       // System.out.println("SOAP "+ soapRequest.writeTo(System.out));

        MimeHeaders headers = soapRequest.getMimeHeaders();
    headers.setHeader("SOAPAction",
          "http://vmi.lt/ais/ws/handleMessage");
           MimeHeaders headers1 = soapRequest.getMimeHeaders();
            headers1.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
        soapRequest.saveChanges();
        soapRequest.writeTo(System.out);
        System.out.println("call request url");








        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(soapRequest, url);
            soapResponse.setProperty("Content-Type", "application/xml; charset=utf-8");
            soapResponse.setContentDescription("application/xml; charset=utf-8");

        ByteArrayOutputStream soapResponseBaos = new ByteArrayOutputStream();
        soapResponse.writeTo(soapResponseBaos);
        String soapResponseXml = soapResponseBaos.toString();

        return soapResponseXml;

        }catch(Exception e){

            return e.toString();
            }
    }

我尝试了许多不同的方法,但是当我直接从 Jdeveloper 调用 Web 服务时它工作正常,但当我从 Oracle Web 逻辑服务器调用时却失败了。

你能帮我解决这个问题吗?

【问题讨论】:

  • 大家好,我已经解决了这个问题。

标签: java oracle web-services soap weblogic


【解决方案1】:

我已解决此问题。请在代码中进行以下更改。

MimeHeaders headers1 = new MimeHeaders();

             headers1.setHeader("Content-Type", "text/xml");
             headers1.setHeader("Encoding", "UTF-8");
//    SOAPMessage soapRequest =
  //      MessageFactory.newInstance().createMessage(new MimeHeaders(),
    //                                               new ByteArrayInputStream(soapRequestXml.getBytes(Charset.forName("UTF-8"))));

    SOAPMessage soapRequest = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(headers1 ,
             new ByteArrayInputStream(soapRequestXml.getBytes(Charset.forName("UTF-8"))));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多