这样一个简单方法,
public void as() throws Exception{ URL url = new URL("http://localhost:8769/accr/print"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.connect(); //获取状态码 int code = urlConnection.getResponseCode(); System.out.println(code); InputStream in = urlConnection.getInputStream(); InputStream raw; if (code == 200) { raw = new BufferedInputStream(in); } else { raw = urlConnection.getErrorStream();// HttpURLConnection 才有getErrorStream方法 } //放入缓存流 //最后使用Reader接收 Reader r = new InputStreamReader(raw); //打印输出 int c; while((c = r.read())>0){ System.out.print((char)c); } System.out.println(); }
竟然报错:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8769/accr/print at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1950) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1945) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1944) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1514) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498) at com.sa.bytebuddy.Test.as(Test.java:183) at com.sa.bytebuddy.Test.testSmoothBurstyRed(Test.java:94) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8769/accr/print at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at com.sa.bytebuddy.Test.as(Test.java:181) ... 23 more
其实原因很简单, URLConnection connect 之后返回 500 的话,是不能执行 getInputStream 方法的。
需要使用getErrorStream方法,但是 URLConnection没有, HttpURLConnection 才有getErrorStream方法。 强制转换一下即可。
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
然后,需要判断 statusCode 是不是200, 否则应该使用
//获取状态码 int code = urlConnection.getResponseCode(); System.out.println(code); InputStream raw; if (code == 200) { InputStream in = urlConnection.getInputStream(); raw = new BufferedInputStream(in); } else { raw = urlConnection.getErrorStream();// HttpURLConnection 才有getErrorStream方法 }