UrlConnection 没有内容类型
Posted
技术标签:
【中文标题】UrlConnection 没有内容类型【英文标题】:UrlConnection no content-type 【发布时间】:2014-06-17 06:05:25 【问题描述】:我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl 连接到传感器并检索值没有问题,但是当我尝试使用 URLConnection 通过 java 连接时出现错误。
我的代码:
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;
错误:
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)
不幸的是,我无法控制响应,因此无法控制设置的标头。此请求返回的唯一标头是状态 - 200 消息。我需要能够读取这些值,但如果没有内容类型,我会迷失方向。有没有更有效的方法来解决这个问题?还是有我只是想念的解决方法?谢谢!
【问题讨论】:
【参考方案1】:如果你不需要依赖服务器返回的内容类型,你应该使用不需要设置内容类型的getInputStream。
【讨论】:
以上是关于UrlConnection 没有内容类型的主要内容,如果未能解决你的问题,请参考以下文章