简体   繁体   中英

UrlConnection no content-type

I am currently writing a web service using Java 7, Maven, Spring MVC, and Eclipselink JPA on Eclipse to access the values of a temperature/humidity sensor connected to an internal network. I have no problem using curl to connect to the sensor and retrieve values, however when I attempt to connect through java using URLConnection I get an error.

My code:

    public SensorReading getReading() {
    SensorReading sr = new SensorReading();

    try {
        String parameters = "a=" + new Date().getTime();
        URL url = new URL(sensorProtocol + sensorAddress + "/readValues?" + parameters);
        URLConnection connection = (URLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        BufferedReader reader = 
                new BufferedReader(new InputStreamReader((InputStream) connection.getContent()));

        sr = parseSensorResponse(reader);

        return esr;
    } catch(IOException e) {
        e.printStackTrace();
        return null;
    }
}

The error:

java.net.UnknownServiceException: no content-type
at java.net.URLConnection.getContentHandler(Unknown Source)
at java.net.URLConnection.getContent(Unknown Source)
at com.company.web.service.SensorReadingServiceImpl.getReading(SensorReadingServiceImpl.java:45)
at com.company.web.service.SensorServiceTest.test(SensorServiceTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Unfortunately I have no control over the response and therefore have no control over what headers are set.. the only header returned with this request is a status - 200 message. I need to be able to read these values, but without the content-type I am rather lost. Is there a more efficient way of going about this? Or is there a work-around that I am just missing? Thanks!

如果您不需要依赖服务器返回的内容类型,则应使用getInputStream,它不需要设置内容类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM