Java1.3下的SoapAction问题
这个问题困扰了我好几天了。希望有人以前遇到过这个问题,并开发了一个解决方法 所以我开发了一个中间件Java应用程序,它在执行过程中调用SOAP操作。现在,这段代码在1.6 JDK下的行为运行良好:Java1.3下的SoapAction问题,java,soap,http-headers,jdk1.4,Java,Soap,Http Headers,Jdk1.4,这个问题困扰了我好几天了。希望有人以前遇到过这个问题,并开发了一个解决方法 所以我开发了一个中间件Java应用程序,它在执行过程中调用SOAP操作。现在,这段代码在1.6 JDK下的行为运行良好: // inside a try-catch block SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
// inside a try-catch block
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMsg = messageFactory.createMessage();
String soapAction = getHeaderUrl() + "/" + method + "\r\n";
MimeHeaders headers = soapMsg.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
// review the SOAPAction header
int header_count =
soapMsg.getMimeHeaders().getHeader("SOAPAction").length;
out("SOAPActions (from Soap header): ");
if (header_count == 0) out ("No SOAPActions defined.");
for (int i=0; i<header_count; i++)
out("SOAPAction["+i+"] = \"" +
soapMsg.getMimeHeaders().getHeader("SOAPAction")[i]
+ "\"");
SOAPPart soapPart = soapMsg.getSOAPPart();
StreamSource s = new StreamSource(new StringReader(msg));
soapPart.setContent(s);
soapMsg.saveChanges();
ByteArrayOutputStream req_bytes = new ByteArrayOutputStream();
soapMsg.writeTo(req_bytes);
String req = new String(req_bytes.toByteArray());
SOAPMessage reply = connection.call(soapMsg, Url);
//在try-catch块中
SOAPConnectionFactory SOAPConnectionFactory=
SOAPConnectionFactory.newInstance();
SOAPConnection连接=
soapConnectionFactory.createConnection();
MessageFactory MessageFactory=MessageFactory.newInstance();
SOAPMessage soapMsg=messageFactory.createMessage();
字符串soapAction=getHeaderUrl()+“/”+method+“\r\n”;
MimeHeaders=soapMsg.getMimeHeaders();
headers.addHeader(“SOAPAction”,SOAPAction);
//查看SOAPAction标题
整数头计数=
soapMsg.getMimeHeaders().getHeader(“SOAPAction”).length;
out(“SOAPActions(来自Soap头):”;
if(header_count==0)out(“未定义SOAPActions”);
对于(inti=0;ispecial),您说您正在SOAP库中链接-确切地说是哪些库
我不想尝试不同的SOAP堆栈,比如CXF或JAX-WS-RI,尽管如果这两个堆栈中的任何一个都支持java 1.3,我会感到惊讶。看起来这实际上可能是一个疏忽或错误,但无论如何,我的解决方法是手动处理较低级别的HTTP内容,即:
URL u = new URL(Url);
URLConnection uc = u.openConnection();
HttpURLConnection conn = (HttpURLConnection) uc;
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("SOAPAction", getHeaderUrl() + "/" + method);
conn.setRequestProperty("Content-type", "text/xml");
OutputStream out = conn.getOutputStream();
Writer wr = new OutputStreamWriter(out);
wr.write(msg);
wr.flush();
wr.close();
InputStream in = conn.getInputStream();
String rsp="";
int c;
while ((c = in.read()) != -1) {
System.out.write(c);
rsp = rsp.concat(String.valueOf((char)c));
}
in.close();
rsp = deSOAP(rsp);
response = response.fromXML(rsp);
return response;
事实证明,这比基于SOAP的解决方案要简洁得多。谢谢。我们将尝试JAX-WS-RI。在最初的文章中还添加了库。