java.io.IOException : HTTP response code: 400 for URL 异常

标签 java ioexception

这是我的示例代码:

String code = request.getParameter("authcode1");
String clientSecret = "xxxxxxxxxxxxx";
String newUrl = "https://accounts.google.com/o/oauth2/token";
String clientId = "xxxxxxxxxxxxxxx";
String callback = "http://localhost:8080/web/guest/home";

String tokenUrl = new String("https://accounts.google.com/o/oauth2/token");

        StringBuffer params = new StringBuffer("");
        params.append("code=" + URLEncoder.encode(code));
        params.append("&client_id=" + clientId);
        params.append("&client_secret=" + clientSecret);
        params.append("&redirect_uri=" + callback);
        params.append("&grant_type=authorization_code");

        try
        {
            // Send data
            URL url = new URL(tokenUrl.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", "0");
                conn.connect();

            InputStream is;
            if (conn.getResponseCode() != 200) {
                is = conn.getInputStream();
            } else {
                is = conn.getErrorStream();
            }

            //URLConnection conn = url.openConnection();
            //conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(params.toString());
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = rd.readLine()) != null)
            {
                System.out.println("-----" + line);
            }
            wr.close();
            rd.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

我在 Tomcat 中遇到以下异常:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://accounts.google.com/o/oauth2/token
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1368)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1362)

请帮我看看我做错了什么?为什么我收到 java.io.IOException:HTTP 响应代码:URL 异常 400?

最佳答案

首先,这是不正确的:

if (conn.getResponseCode() != 200) {
                is = conn.getInputStream();
            } else {
                is = conn.getErrorStream();
            }

应该是

if (conn.getResponseCode() == 200) {
                is = conn.getInputStream();
            } else {
                is = conn.getErrorStream();
            }

您的问题在这一行:

conn.setRequestProperty("Content-Length", "0");

当您在 HTTP 消息中附加了实体主体时,您不能发送 Content-Length0

而是将其设置为

conn.setRequestProperty("Content-Length", params.toString().length());

关于java.io.IOException : HTTP response code: 400 for URL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10976028/

相关文章:

java - java.io.File.createNewFile() 处的 IOException;

Android 从内部存储播放资源文件导致 MediaPlayer.prepare 给出 IOException

java - 场景生成器,如何在按钮单击时添加多个任务

java - 用 Java 调用 Oracle 存储过程

java - 将大文件拆分成 block

Android 蓝牙 .connect() 异常 Nexus 7 OBDII 适配器

java - 接口(interface)抽象之间的区别(不重复)

java - 如何在调整窗口大小时重新定位 JButton

java - 操作监听器/抛出 IOException 冲突

Java 可序列化问题