【发布时间】:2012-10-01 10:18:18
【问题描述】:
我想将 XML 文件作为请求发送到 SOAP 服务器。 这是我的代码:(修改自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”(内部服务器错误),但现在我根本没有得到任何回复。我知道服务器工作正常,因为其他客户端没有问题。
谢谢。
【问题讨论】:
-
你说的“之前”是什么意思?如果你发送一个有效的 xml 字符串而不是
xml here会发生什么? -
请说明您是否使用IDE,如果是,那么。如果您使用的是诸如 Oracle Jdeveloper 11g 之类的 IDE,那么只需导入 WSDL,IDE 就会自动生成代码。
-
您应该发布请求的正文。如果您的 xml 有问题,您可能不会得到任何响应。
标签: java http post soap apache-commons-httpclient