我想将一个XML文件作为请求发送到SOAP服务器.这是我的代码:( 使用org.apache.http从使用SOAP操作发送HTTP Post请求进行了修改)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
在此之前我从服务器收到错误'http 500'(内部服务器错误),但现在我没有得到任何回复.我知道服务器工作正常,因为与其他客户端没有问题.
谢谢.