Java-URLConnection返回错误代码500

时间:2019-12-08 15:32:40

标签: java httpurlconnection

因此,我尝试将请求发送到我的网站并读取返回的响应(JSON数组) 网站输出示例:

[
  "xxx",
  "xxx",
  "xxx",
  "xxx",
  "xxx"
]

这是我请求并阅读它的代码->

        public static void main(String[] args) throws Exception{
        URL url = new URL("example.com/getJsonResponse");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 1.0.3705)");
        System.out.println(ApiRequest.getResponse(connection));
    }

引发以下错误:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: example.com/getJsonResponse

getResponse方法:

    static String getResponse(URLConnection connection) throws IOException{
        Scanner scanner = new Scanner(connection.getInputStream());
        StringBuilder builder = new StringBuilder();
        while (scanner.hasNextLine()) {
            builder.append(scanner.nextLine());
        }
        scanner.close();
        return ((builder.toString() != null) ? builder.toString() : "null");
    }

(我已经使用pastebin糊测试了上面的方法,并且效果很好)

因此,当我手动访问该网站时,该网站返回的JSON响应很好。

需要任何其他信息让我知道。

0 个答案:

没有答案