【问题标题】:java.io.IOException: HTTP response code: 400 for URLjava.io.IOException:HTTP 响应代码:URL 为 400
【发布时间】:2012-06-11 07:31:58
【问题描述】:

这是我的示例代码:

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 response code: 400 for URL exception?

【问题讨论】:

  • 你检查过 400 是什么意思吗?您是否通过浏览器尝试过。检查这个:?checkupdown.com/status/E400.html
  • 当 conn.getResponseCode() != 200 你将处理输入流?这不应该是相反的吗?

标签: java ioexception


【解决方案1】:

首先,这是不正确的:

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());

【讨论】:

  • 感谢所有 cmets。我确实将 content-length 更改为 params.toString.length() 和 conn.getResponseCode() == 200 但现在我得到 java.net.ProtocolException: Cannot write output after reading input。在 sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:900) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230) 我在行出现异常:OutputStreamWriter wr = 新的 OutputStreamWriter(conn.getOutputStream());这里有什么问题?
  • 很简单。您首先写信给OutputStreamInputStream阅读。你正在做相反的事情。
  • @精英绅士。谢谢。现在我成功地从服务器获得了 access_token。
  • 然后接受答案(分数下方的勾号)以确认答案确实对您有帮助。
  • @user1443749 您根本不需要设置 Content-Length。 Java 会为你做这件事。正确。你自己设定的任何东西都可能是错误的。尤其是零。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2015-09-04
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多