读书人

50分求webservices一个有关问题

发布时间: 2011-12-24 23:03:24 作者: rapoo

50分求webservices一个问题?
服务端(没有异常,一切正常):

package untitled7;

public class Bean1 {
String m = " ";
public Bean1() {
}

public String getM() {
return m;
}


public void setM(String s) {
m = s;
}
}

wdsl================================================:

<?xml version= "1.0 " encoding= "UTF-8 " ?>
- <wsdl:definitions targetNamespace= "http://untitled7 " xmlns:apachesoap= "http://xml.apache.org/xml-soap " xmlns:impl= "http://untitled7 " xmlns:intf= "http://untitled7 " xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/ " xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/ " xmlns:wsdlsoap= "http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:xsd= "http://www.w3.org/2001/XMLSchema ">
- <!--
WSDL created by Apache Axis version: 1.2beta3
Built on Aug 01, 2004 (05:59:22 PDT)

-->
- <wsdl:message name= "setMRequest ">
<wsdl:part name= "s " type= "soapenc:string " />
</wsdl:message>
- <wsdl:message name= "getMResponse ">
<wsdl:part name= "getMReturn " type= "soapenc:string " />
</wsdl:message>
<wsdl:message name= "setMResponse " />
<wsdl:message name= "getMRequest " />
- <wsdl:portType name= "Bean1 ">
- <wsdl:operation name= "getM ">
<wsdl:input message= "impl:getMRequest " name= "getMRequest " />
<wsdl:output message= "impl:getMResponse " name= "getMResponse " />
</wsdl:operation>
- <wsdl:operation name= "setM " parameterOrder= "s ">
<wsdl:input message= "impl:setMRequest " name= "setMRequest " />
<wsdl:output message= "impl:setMResponse " name= "setMResponse " />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name= "Bean1SoapBinding " type= "impl:Bean1 ">
<wsdlsoap:binding style= "rpc " transport= "http://schemas.xmlsoap.org/soap/http " />
- <wsdl:operation name= "getM ">
<wsdlsoap:operation soapAction= " " />
- <wsdl:input name= "getMRequest ">
<wsdlsoap:body encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ " namespace= "http://untitled7 " use= "encoded " />


</wsdl:input>
- <wsdl:output name= "getMResponse ">
<wsdlsoap:body encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ " namespace= "http://untitled7 " use= "encoded " />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name= "setM ">
<wsdlsoap:operation soapAction= " " />
- <wsdl:input name= "setMRequest ">
<wsdlsoap:body encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ " namespace= "http://untitled7 " use= "encoded " />
</wsdl:input>
- <wsdl:output name= "setMResponse ">
<wsdlsoap:body encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ " namespace= "http://untitled7 " use= "encoded " />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name= "Bean1Service ">
- <wsdl:port binding= "impl:Bean1SoapBinding " name= "Bean1 ">
<wsdlsoap:address location= "http://localhost:8080/axis/services/Bean1 " />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端:

package untitled8;


import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;

public class Test {

public static void main(String[] args) throws Exception {

URL url = new URL( "http://10.0.0.166:8080/axis/services/Bean1?wsdl "); //这边是否有错?????????该怎么写??
SOAPMappingRegistry smr = new SOAPMappingRegistry();
StringDeserializer sd = new StringDeserializer();
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName( " ", "Result "), null, null,
sd);

// 创建传输路径和参数
SOAPHTTPConnection st = new SOAPHTTPConnection();

// 创建调用
Call call = new Call();
call.setSOAPTransport(st);
call.setSOAPMappingRegistry(smr);



call.setTargetObjectURI( "http://untitled7 ");//这边是否有错?????????该怎么写??
call.setMethodName( "setM ");
call.setEncodingStyleURI( "http://schemas.xmlsoap.org/soap/encoding/ ");

Vector params = new Vector();
params.addElement(new Parameter( "s ", String.class, "hzc ", null));
call.setParams(params);

Response resp = null;

try {
resp = call.invoke(url, "http://untitled7.setM ");//这边是否有错?????????该怎么写??
System.out.println(resp);//见下文打印
} catch (SOAPException e) {
System.err.println( "Caught SOAPException ( " + e.getFaultCode() +
"): " + e.getMessage());
return;
}

// 检查返回值
if (resp != null && !resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
try {//这边有异常
Object value = ret.getValue();
System.out.println( "Answer--> " + value);

} catch (Exception er) {
System.err.println(er.getMessage());//后打印null
}

} else {
Fault fault = resp.getFault();
System.err.println( "Generated fault: ");
System.out.println( " Fault Code = " + fault.getFaultCode());


System.out.println( " Fault String = " + fault.getFaultString());
}
}
}


后台打印出
[Header=null] [methodName=setM] [targetObjectURI=http://untitled7] [encodingStyleURI=http://schemas.xmlsoap.org/soap/encoding/] [SOAPContext=[Parts={[cid:null type: text/xml; charset=utf-8 enc: null]}]] [return=null]
[Params={}]

null


望高手指点。???

[解决办法]

public String webServiceCall(String targetEndpointAddress,
String operationName,
String requestInfo) {
//System.out.println( "15 requestInfo = " + requestInfo);
String returnStr = " ";
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(targetEndpointAddress)); //设置web service URL 地址
call.setOperationName(new QName(targetEndpointAddress,operationName)); //设置操作的名称。
String responeInfo = (String) call.invoke(new Object[] {requestInfo});
returnStr = responeInfo;

}
catch (Exception e) {
e.printStackTrace();
}

return returnStr;
}


[解决办法]
标记一下,有空看

读书人网 >Java Web开发

热点推荐