我想将 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”(内部服务器错误),但现在我根本没有得到任何回复。我知道服务器工作正常,因为其他客户端没有问题。
谢谢。