• 记1个低级错误:java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8769/accr/print


    这样一个简单方法,

        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方法
            }


    版权声明
    本文原创发表于 博客园,作者为 阿K .     本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
    欢迎关注本人微信公众号:觉醒的码农,或者扫码进群:

  • 相关阅读:
    python流行的原因
    shell rename directory
    shell if [ -d filename]
    eclipse文本编码格式修改为UTF-8
    egrep 第几列开始
    Java double 精度
    BigDecimal 两种方式
    使用SecureCRT连接ubuntu
    eclipse快速查找一个变量、方法或者类被引用的地方
    我的互联网金融行业经验总结
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/14494092.html
Copyright © 2020-2023  润新知