Java 使用Apache http客户端向SOAP web服务发送请求
我正在使用ApacheHttpClient编写一个Java代码来调用SOAP web服务。到目前为止,我已经写了下面的代码,它工作得很好Java 使用Apache http客户端向SOAP web服务发送请求,java,web-services,soap,apache-commons-httpclient,Java,Web Services,Soap,Apache Commons Httpclient,我正在使用ApacheHttpClient编写一个Java代码来调用SOAP web服务。到目前为止,我已经写了下面的代码,它工作得很好 void post(String strURL, String strSoapAction, String strXMLFilename) throws Exception{ File input = new File(strXMLFilename); PostMethod post = new PostMe
void post(String strURL, String strSoapAction, String strXMLFilename) throws Exception{
File input = new File(strXMLFilename);
PostMethod post = new PostMethod(strURL);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", strSoapAction);
HttpClient httpclient = new HttpClient();
try {
int result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
}
这里,strXMLFilename是取自soapui的SOAP请求文件,我复制了soapui请求XML并将其粘贴到一些新的XML文件中,然后使用它。
上面测试了一个以字符串作为参数的服务
但现在我的问题是,它是否能够测试以文件作为输入的服务?我需要向服务中心发送一个文件。在SOAPUI中,我可以附加一个文件并测试服务,但不能对上面的代码执行同样的操作。好的,伙计,试试这个,它对我来说很好
void sendRequest()throws Exception {
System.out.println("Start sending " + action + " request");
URL url = new URL( serviceURL );
HttpURLConnection rc = (HttpURLConnection)url.openConnection();
//System.out.println("Connection opened " + rc );
rc.setRequestMethod("POST");
rc.setDoOutput( true );
rc.setDoInput( true );
rc.setRequestProperty( "Content-Type", "text/xml; charset=utf-8" );
String reqStr = reqT.getRequest();
int len = reqStr.length();
rc.setRequestProperty( "Content-Length", Integer.toString( len ) );
rc.setRequestProperty("SOAPAction", soapActions[actionLookup(action)] );
rc.connect();
OutputStreamWriter out = new OutputStreamWriter( rc.getOutputStream() );
out.write( reqStr, 0, len );
out.flush();
System.out.println("Request sent, reading response ");
InputStreamReader read = new InputStreamReader( rc.getInputStream() );
StringBuilder sb = new StringBuilder();
int ch = read.read();
while( ch != -1 ){
sb.append((char)ch);
ch = read.read();
}
response = sb.toString();
read.close();
rc.disconnect();
}
调用getRequest的“reqT”对象是什么??