获取javax.xml.soap.SOAPException:通过Java调用Web服务时,Oracle Weblogi



我用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逻辑服务器调用时失败。

你能帮我这个吗?

我已经解决了这个问题。请在代码中进行以下更改。

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"))));

相关内容

  • 没有找到相关文章

最新更新