问题描述
在使用SAAJ调用http://WebXml.com.cn/中的web服务的时候,服务器返回“soap:Server服务器无法处理请求。--->未将对象引用设置到对象的实例。”各位帮忙看一下问题出在哪里?附:调用自己用JAX-WS建立的WebService没有问题。代码如下:importjavax.xml.soap.MessageFactory;importjavax.xml.soap.MimeHeaders;importjavax.xml.soap.SOAPBody;importjavax.xml.soap.SOAPConnection;importjavax.xml.soap.SOAPConnectionFactory;importjavax.xml.soap.SOAPElement;importjavax.xml.soap.SOAPEnvelope;importjavax.xml.soap.SOAPMessage;importjavax.xml.soap.SOAPPart;importjavax.xml.transform.Source;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.stream.StreamResult;publicclassSOAPTip{publicstaticvoidmain(Stringargs[]){try{//FirstcreatetheconnectionSOAPConnectionFactorysoapConnFactory=SOAPConnectionFactory.newInstance();SOAPConnectionconnection=soapConnFactory.createConnection();//Next,createtheactualmessageMessageFactorymessageFactory=MessageFactory.newInstance();SOAPMessagemessage=messageFactory.createMessage();MimeHeadershd=message.getMimeHeaders();hd.addHeader("SOAPAction","http://WebXml.com.cn/getWeatherbyCityName");message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING,"gb2312");SOAPPartsoapPart=message.getSOAPPart();//CreateobjectsforthemessagepartsSOAPEnvelopeenvelope=soapPart.getEnvelope();/*envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");envelope.addNamespaceDeclaration("enc","http://schemas.xmlsoap.org/soap/encoding/");envelope.addNamespaceDeclaration("env","http://schemas.xmlsoap.org/soap/envelop/");envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");*/SOAPBodybody=envelope.getBody();//Populatethebody//CreatethemainelementandnamespaceSOAPElementbodyElement=body.addChildElement(envelope.createName("getWeatherbyCityName","ns1","http://WebXml.com.cn/"));//AddcontentStringcity="北京";bodyElement.addChildElement("theCityName").addTextNode(city);//Savethemessagemessage.saveChanges();//ChecktheinputSystem.out.println("\nREQUEST:\n");message.writeTo(System.out);System.out.println();//Sendthemessageandgetareply//SetthedestinationStringdestination="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";//SendthemessageSOAPMessagereply=connection.call(message,destination);//ChecktheoutputSystem.out.println("\nRESPONSE:\n");//CreatethetransformerTransformerFactorytransformerFactory=TransformerFactory.newInstance();Transformertransformer=transformerFactory.newTransformer();//ExtractthecontentofthereplySourcesourceContent=reply.getSOAPPart().getContent();//SettheoutputforthetransformationStreamResultresult=newStreamResult(System.out);transformer.transform(sourceContent,result);System.out.println();Stringresponse=reply.getSOAPBody().getTextContent();System.err.println(response);//Closetheconnectionconnection.close();}catch(Exceptione){System.out.println(e.getMessage());}}}