我想送一个XML文件作为SOAP服务器的请求。 下面是代码我有:(从改性发送HTTP POST请求使用SOAP动作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”(内部服务器错误),但我现在没有得到任何答复的。 我知道服务器运行正常。因为与其他客户端是没有问题的。
谢谢。
org.apache.http API不是SOAP / Web服务感知,所以你在做一个非标准的方式棘手的工作。 该代码是不是很Java友好的或柔性的,因为它不能自动“绑定”(转换)java对象数据到SOAP请求和流出SOAP响应的。 这是一个有点冗长,棘手的调试和获得工作,而脆 - 你处理的全SOAP协议,包括故障处理等?
我可以建议使用JAX-WS标准,这是内置到JVM:
1.保存WSDL文件到本地磁盘
例如<app path>/META-INF/wsdl/abc.com/calculator/Calculator.wsdl
如果您没有WSDL,你可以输入到浏览器和结果页面保存到磁盘:
http://abc.com/calculator/Calculator?wsdl
2.使用的wsimport命令转换WSDL到Java类文件
对于JDK,工具处于<jdkdir>\bin\wsimport.exe (or .sh)
为应用服务器,将会像<app_server_root>\bin\wsimport.exe (or .sh)
<filepath>\wsimport -keep -verbose <wsdlpath>\Calculator.wsdl
或者如果你的WSDL通过预先存在的web服务可用
<filepath>\wsimport -keep -verbose http://abc.com/calculator/Calculator?wsdl
(也可以包括“-p com.abc.calculator”来设置生成的类的封装)
像下面的生成文件 - 包括在你的Java项目中的这些源文件:
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();
}
}
}
尝试发送这样的请求。 这是我做的最后一次:
try
{
StringBuffer strBuffer = new StringBuffer();
HttpURLConnection connection = connectToEndPoint(endpoint);
OutputStream outputStream = generateXMLOutput(connection, yourvalue, strDate);
InputStream inputStream = connection.getInputStream();
int i;
while ((i = inputStream.read()) != -1) {
Writer writer = new StringWriter();
writer.write(i);
strBuffer.append(writer.toString());
String status = xmlOutputParse(strBuffer);
而且使用的功能:
private static HttpURLConnection connectToEndPoint(String wsEndPoint) throws MalformedURLException, IOException {
URL urlEndPoint = new URL(wsEndPoint);
URLConnection urlEndPointConnection = urlEndPoint.openConnection();
HttpURLConnection httpUrlconnection = (HttpURLConnection) urlEndPointConnection;
httpUrlconnection.setDoOutput(true);
httpUrlconnection.setDoInput(true);
httpUrlconnection.setRequestMethod("POST");
httpUrlconnection.setRequestProperty("content-type", "application/soap+xml;charset=UTF-8");
// set connection time out to 2 seconds
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(2 * 1000));
// httpUrlconnection.setConnectTimeout(2*1000);
// set input stream read timeout to 2 seconds
System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(2 * 1000));
// httpUrlconnection.setReadTimeout(2*1000);
return httpUrlconnection;
}
当您手动创建XML(修改您的需要):
private static OutputStream generateXMLOutput(HttpURLConnection conn, String msisdn, String strDate) throws IOException {
OutputStream outputStream = conn.getOutputStream();
StringBuffer buf = new StringBuffer();
buf.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ins=\"http://yournamespace">\r\n");
buf.append("<soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\r\n");
//..... append all your lines .......
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ins=\"http://yournamespace\">\r\n");
outputStreamWriter.write("<soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\r\n");
//..... write all your lines .......
outputStreamWriter.flush();
outputStream.close();
return outputStream;
}
和回放功能的WS回答:
private static String xmlOutputParse(StringBuffer xmlInputParam) throws IOException, ParserConfigurationException, SAXException {
String status = null;
DocumentBuilderFactory docBuilderfFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = docBuilderfFactory.newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(xmlInputParam.toString()));
Document document = documentBuilder.parse(inputSource);
NodeList nodeList = document.getElementsByTagName("ResponseHeader");
Element element = (Element) nodeList.item(0);
if (element == null) {
return null;
}
NodeList name = element.getElementsByTagName("Status");
Element line = (Element) name.item(0);
if (line == null) {
return null;
}
if (line.getFirstChild() instanceof CharacterData) {
CharacterData cd = (CharacterData) line.getFirstChild();
status = cd.getData().trim();
}
return status;
}
我认为,这个解决方案(即使是长)适用于大多数情况下。 我希望你能使其适应您的需求。
最好的祝福 !