问题描述
我想将XML文件作为请求发送到SOAP服务器。
这是我的代码:(修改自)
I want to send an XML file as a request to a SOAP server.Here is the code I have: (modified from Sending HTTP Post request with SOAP action using org.apache.http )
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.HttpResponse;
import java.net.URI;
public static void req() {
try {
HttpClient httpclient = new DefaultHttpClient();
String body="xml here";
String bodyLength=new Integer(body.length()).toString();
URI uri=new URI("http://1.1.1.1:100/Service");
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader( "SOAPAction", "MonitoringService" );
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
StringEntity entity = new StringEntity(body, "text/xml",HTTP.DEFAULT_CONTENT_CHARSET);
httpPost.setEntity(entity);
RequestWrapper requestWrapper=new RequestWrapper(httpPost);
requestWrapper.setMethod("POST");
requestWrapper.setHeader("Content-Length",bodyLength);
HttpResponse response = httpclient.execute(requestWrapper);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
在此之前我收到错误'http 500'(来自服务器的内部服务器错误)但现在我根本没有收到任何回复。我知道服务器工作正常,因为与其他客户端没有问题。
Before this I was getting error 'http 500' (internal server error) from the server , but now Im not getting any reply at all. I know that the server works right because with other clients there is no problem.
谢谢。
推荐答案
org.apache.http API不是感知SOAP / Web服务,因此您以非标准方式执行棘手的工作。代码不是非常友好的或灵活的,因为它不能自动将Java对象数据绑定(转换)成SOAP请求和SOAP响应。它有点冗长,调试和工作都很棘手,而且很脆弱 - 您是在处理完整的SOAP协议,包括故障处理等吗?
org.apache.http API is not SOAP/web service aware and so you're doing the tricky work in a non-standard way. The code is not very java-friendly or flexible, because it can't automatically "bind" (convert) java object data into the SOAP request and out of the SOAP response. It's a little lengthy, tricky to debug and get working, and brittle - are you handling the full SOAP protocol, including fault handling, etc?.
我可以建议使用JAX吗? -WS标准,内置于JVM中:
Can I suggest using JAX-WS standard, which is built into the JVM:
1。将WSDL文件保存到本地磁盘
例如< app path> /META-INF/wsdl/abc.com/calculator/Calculator.wsdl
如果您没有WSDL,你可以输入浏览器&将结果页面保存到磁盘:
2。使用wsimport命令将WSDL转换为java类文件
对于JDK,工具位于< jdkdir> \bin\wsimport.exe(或.sh)中
。
对于应用服务器,将类似于< app_server_root> \bin \wsimport.exe(或.sh)
< filepath> \wsimport -keep -verbose< wsdlpath> \ Calcallator.wsdl
或者,如果您的WSDL可通过预先存在的网络服务获得
OR if your WSDL is available via a pre-existing webservice
< filepath> \wsimport -keep -verbose http://abc.com/calculator/Calculator?wsdl
(你也可以包括-p com.abc.calculator来设置生成的类的包)
(you can also include "-p com.abc.calculator" to set the package of generated classes)
生成如下文件 - 在java项目中包含这些源文件: / p>
Files like the following are generated - include these source files in your java project:
com\abc\calculator\ObjectFactory.java
com\abc\calculator\package-info.java
com\abc\calculator\Calculator.java ............................name = `<wsdl:portType>` name attribute
com\abc\calculator\CalculatorService.java ................name = `<wsdl:service>` name attribute
com\abc\calculator\CalculatorRequestType.java .......name = schema type used in input message
com\abc\calculator\CalculatorResultType.java ..........name = schema type used in output message
2。创建JAX-WS SOAP Web服务客户端
package com.abc.calculator.client;
import javax.xml.ws.WebServiceRef;
import com.abc.calculator.CalculatorService;
import com.abc.calculator.Calculator;
public class CalculatorClient {
@WebServiceRef(wsdlLocation="META-INF/wsdl/abc.com/calculator/Calculator.wsdl")
// or @WebServiceRef(wsdlLocation="http://abc.com/calculator/Calculator?wsdl")
public static CalculatorService calculatorService;
public CalculatorResponseType testCalculation() {
try {
CalculatorRequestType request = new CalculatorRequest();
request.setSomeParameter("abc");
request.setOtherParameter(3);
Calculator calculator = calculatorService.getCalculatorPort();
// automatically generate SOAP XML message, send via HTTP,
// receive & marshal response to java object
String response = calculator.doCalculation(response);
} catch(Exception e) {
e.printStackTrace();
}
}
}
这篇关于为什么这个简单的SOAP客户端不工作(org.apache.http)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!