[英]Getting javax.xml.soap.SOAPException: Content-Type not specified error on Oracle weblogic server while calling web service through Java
I have written a java code using Jdeveloper tool to call external web service. 我已经使用Jdeveloper工具编写了一个Java代码来调用外部Web服务。 When I am running my code from Jdeveloper it is working fine.
当我从Jdeveloper运行我的代码时,它运行良好。 But I am deploying my code on Oracle weblogic 10.3.6 server and try to run this code then It is giving me below error -
但是我将我的代码部署在Oracle weblogic 10.3.6服务器上,然后尝试运行此代码,然后出现以下错误-
javax.xml.soap.SOAPException: Content-Type not specified javax.xml.soap.SOAPException:未指定Content-Type
Here is my code - 这是我的代码-
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();
}
}
I have tried many different things but it works fine when i call web service directly from Jdeveloper but fails when I call from Oracle web logic server. 我已经尝试了许多不同的方法,但是当我直接从Jdeveloper调用Web服务时,它工作正常,但是当我从Oracle Web逻辑服务器调用时,它却失败了。
Could you please help me with this? 你能帮我这个忙吗?
I have fixed this issue.Please make following changes in the code. 我已解决此问题。请在代码中进行以下更改。
MimeHeaders headers1 = new MimeHeaders(); MimeHeaders headers1 =新的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"))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.