0

我正在研究 SOAP 和 java,我必须连接到 NDFD NOAA 服务器并向其发出 XML 请求并从服务器获取一些信息。

以下是我的代码和我得到的错误。

        SOAPConnectionFactory lab3_factory = SOAPConnectionFactory.newInstance();
        SOAPConnection lab3_conn = lab3_factory.createConnection();
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        SOAPPart part = msg.getSOAPPart();

        SOAPEnvelope envelope = part.getEnvelope();

        Name xsi = envelope.createName("type","xsi","http://www.w3.org/2001/XMLSchema-instance");
        //QName xsi = new QName("xsi:type");
        Name xsd = envelope.createName("lab_3","xsd","http://www.w3.org/2001/XMLSchema");
        String ns6244_serverURI = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl";
        envelope.addNamespaceDeclaration("ns6244",ns6244_serverURI);
        //String xsi_serverURI = "http://www.w3.org/2001/XMLSchema-instance";
        //envelope.addNamespaceDeclaration("xsi",xsi_serverURI);


        envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); 
        SOAPBody body = envelope.getBody();


        SOAPElement soapBodyElem = body.addChildElement("NDFDgenByDay", "ns6244");
        //String encoding_string = "http://schemas.xmlsoap.org/soap/encoding/";
        //soapBodyElem.setEncodingStyle(encoding_string);

        Name latitude = envelope.createName("latitude");

        SOAPElement soapBodyElem1 = body.addChildElement(latitude);
        soapBodyElem1.addAttribute(xsi,"xsd:decimal");
        BigDecimal x = new BigDecimal(35.4);
        soapBodyElem1.setValue(x.toString());

        Name longitude = envelope.createName("longitude");

        SOAPElement soapBodyElem2 = body.addChildElement(longitude);
        soapBodyElem2.addAttribute(xsi,"xsd:decimal");
        BigDecimal y = new BigDecimal(-97.6);
        soapBodyElem2.setValue(y.toString());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();

        GregorianCalendar c = new GregorianCalendar();
        c.setTime(date);
        XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        String upd_date = "\0";
        String d2 = date2.toString();
        for(String d:d2.split("T"))
        {
            upd_date = d;
            break;
        }
        Name startDate = envelope.createName("startDate");
        SOAPElement soapBodyElem4 = body.addChildElement(startDate);
        soapBodyElem4.addAttribute(xsi,"xsd:date");
        soapBodyElem4.setValue(upd_date);


        Name numDays = envelope.createName("numDays");
        SOAPElement soapBodyElem5 = body.addChildElement(numDays);
        soapBodyElem5.addAttribute(xsi,"xsd:integer");
        soapBodyElem5.setValue("3");



        Name format = envelope.createName("format");

        SOAPElement soapBodyElem3 = body.addChildElement(format);
        soapBodyElem3.addAttribute(xsi,"dwml:formatType");
        soapBodyElem3.addNamespaceDeclaration("dwml","http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd");
        soapBodyElem3.setValue("12 hourly");


        SOAPHeader header = envelope.getHeader();


        /*
        SOAPBodyElement element = body.addBodyElement(envelope.createName("JAVA", "training", "http://JitendraZaa.com/blog"));
        element.addChildElement("WS").addTextNode("Training on Web service");

        SOAPBodyElement element1 = body.addBodyElement(envelope.createName("JAVA", "training", "http://JitendraZaa.com/blog"));
        element1.addChildElement("Spring").addTextNode("Training on Spring 3.0");
        */
        msg.writeTo(System.out);
        msg.saveChanges();

        FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
        msg.writeTo(fOut);

        String getdata_url = "https://graphical.weather.gov:443/xml/SOAP_server/ndfdXMLserver.php";

        String url_header = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl";

        MimeHeaders headers = msg.getMimeHeaders();
        headers.addHeader("SOAPAction", url_header  + "NDFDgenByDay");


        SOAPMessage msg_reply = lab3_conn.call(msg,getdata_url);

        msg_reply.writeTo(System.out);

        System.out.println();
        System.out.println("SOAP msg created");

我得到的错误是..

    </SOAP-ENV:Envelope><?xml version="1.0" encoding="ISO-8859-1"?>
    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
     <SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SERVER</faultcode><faultactor xsi:type="xsd:string">   

   </faultactor><faultstring xsi:type="xsd:string">format needs to be either 24 hourly or 12 hourly</faultstring>
  <detail xsi:type="xsd:string">input format was &quot;&quot;</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

任何形式的帮助表示赞赏。

4

1 回答 1

0

您的代码有很多问题,甚至没有以 web 服务调用所需的格式创建请求 SOAP 信封。

试试下面的代码

 SOAPConnectionFactory lab3_factory = SOAPConnectionFactory.newInstance();
         SOAPConnection lab3_conn = lab3_factory.createConnection();
         MessageFactory factory = MessageFactory.newInstance();
         SOAPMessage msg = factory.createMessage();
         SOAPPart part = msg.getSOAPPart();

         SOAPEnvelope envelope = part.getEnvelope();

         Name xsi = envelope.createName("type","xsi","http://www.w3.org/2001/XMLSchema-instance");
         //QName xsi = new QName("xsi:type");
         Name xsd = envelope.createName("lab_3","xsd","http://www.w3.org/2001/XMLSchema");
         String ns6244_serverURI = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl";
         envelope.addNamespaceDeclaration("ns6244",ns6244_serverURI);
         envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); 
         SOAPBody body = envelope.getBody();

         SOAPElement soapBodyElem = body.addChildElement("NDFDgenByDay", "ns6244");

         SOAPElement soapBodyElem1 = soapBodyElem.addChildElement(new QName("latitude"));         
         BigDecimal x = new BigDecimal(35.4);
         soapBodyElem1.setValue(x.toString());  
         SOAPElement soapBodyElem2 = soapBodyElem.addChildElement(new QName("longitude"));        
         BigDecimal y = new BigDecimal(-97.6);
         soapBodyElem2.setValue(y.toString());
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
         Date date = new Date();
         GregorianCalendar c = new GregorianCalendar();
         c.setTime(date);
         XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
         String upd_date = "\0";
         String d2 = date2.toString();
         for(String d:d2.split("T"))
         {
             upd_date = d;
             break;
         }

         SOAPElement soapBodyElem4 = soapBodyElem.addChildElement(new QName("startDate"));         
         soapBodyElem4.setValue(upd_date);         
         SOAPElement soapBodyElem5 = soapBodyElem.addChildElement(new QName("numDays"));         
         soapBodyElem5.setValue("3");
         SOAPElement soapBodyElem6 = soapBodyElem.addChildElement(new QName("Unit"));
         soapBodyElem6.setValue("e");         
         SOAPElement soapBodyElem3 = soapBodyElem.addChildElement(new QName("format"));
         soapBodyElem3.setValue("12 hourly");
         SOAPHeader header = envelope.getHeader();       
         msg.writeTo(System.out);
         msg.saveChanges();
         String getdata_url = "https://graphical.weather.gov:443/xml/SOAP_server/ndfdXMLserver.php";
         String url_header = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl";
         MimeHeaders headers = msg.getMimeHeaders();
         headers.addHeader("SOAPAction", url_header  + "NDFDgenByDay");
         SOAPMessage msg_reply = lab3_conn.call(msg,getdata_url);

         msg_reply.writeTo(System.out);

        System.out.println();
于 2017-05-05T13:32:39.567 回答