如何在Java中修复“ SAAJ0537:无效的内容类型”

时间:2019-01-28 20:09:18

标签: java soap noaa

我正在开发一个需要获取当前和历史天气数据的项目,到目前为止,我一直在尝试使用库https://github.com/sudohippie/weather-noaa,对其进行修改以与Java 8一起使用(最后一个提交是在2013年),但我似乎无法使其SOAP客户端正常工作。相关代码和错误日志如下。

package sudoHippieNOAA.service.soap;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Raghav Sidhanti
 * 9/12/13
 */
public class SOAPClientTest {
    public static void main(String[] args) throws IOException, SOAPException {
        String endpoint = " https://graphical.weather.gov:443/xml/SOAP_server/ndfdXMLserver.php";
        String methodName = "GmlTimeSeries";

        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("listLatLon", "38.99,-77.02 39.70,-104.80");
        params.put("startTime", "2004-01-01T00:00:00");
        params.put("endTime", "2017-09-12T00:00:00");
        params.put("compType", "Between");
        params.put("featureType", "Forecast_Gml2Point");
        params.put("propertyName", "maxt,mint,wx");

        SOAPClient client = new SOAPClient(endpoint);
        SOAPMessage message = client.getDataAsSOAPMessage(methodName, params);

        System.out.println(NOAASOAPUtil.getSOAPMessageContentAsString(message));
    }
}
package sudoHippieNOAA.service.soap;

import javax.xml.soap.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Raghav Sidhanti
 * 9/11/13
 */
public class SOAPClient {
    private final String endpoint;

    public SOAPClient(String endpoint) {
        this.endpoint = endpoint;
    }

    public SOAPMessage getDataAsSOAPMessage(String methodName, Map<String, String> args) throws MalformedURLException, SOAPException {
        // construct message
        SOAPMessage requestMessage = buildMessage(methodName, args);

        // connect to server and retrieve information
        SOAPMessage responseMessage = requestForData(requestMessage);

        return responseMessage;
    }

    private SOAPMessage requestForData(SOAPMessage message) throws SOAPException, MalformedURLException {
        SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = connectionFactory.createConnection();

        try{
            return connection.call(message, new URL(endpoint));
        }finally {
            connection.close();
        }
    }

    private SOAPMessage buildMessage(String methodName, Map<String, String> args) throws SOAPException {
        // construct a message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();

        // getData message envelope and body
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();

        // create body element
        Name bodyName = envelope.createName(methodName, "m", endpoint);
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

        // add parameters as children of body element
        for(String param : args.keySet()){
            Name name = envelope.createName(param);
            bodyElement.addChildElement(name).addTextNode(args.get(param));
        }

        message.saveChanges();

        return message;
    }

    String getEndpoint() {
        return endpoint;
    }
}
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source)
    at sudoHippieNOAA.service.soap.SOAPClient.requestForData(SOAPClient.java:34)
    at sudoHippieNOAA.service.soap.SOAPClient.getDataAsSOAPMessage(SOAPClient.java:24)
    at sudoHippieNOAA.service.soap.SOAPClientTest.main(SOAPClientTest.java:27)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source)
    ... 4 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source)
    at sudoHippieNOAA.service.soap.SOAPClient.requestForData(SOAPClient.java:34)
    at sudoHippieNOAA.service.soap.SOAPClient.getDataAsSOAPMessage(SOAPClient.java:24)
    at sudoHippieNOAA.service.soap.SOAPClientTest.main(SOAPClientTest.java:27)

有人知道问题是什么,如何解决,还是任何维护得更好并且具有相同目的的库(请注意:只要我可以获得雷达反射率数据以及理想的湿度和温度,它就可以工作)?它不需要从NOAA提取数据。 WeatherUnderground或类似服务也可以。

0 个答案:

没有答案