简体   繁体   English

java.io.IOException:HTTP响应代码:400用于URL

[英]java.io.IOException: HTTP response code: 400 for URL

Here is my sample code : 这是我的示例代码:

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

I am getting following exception in Tomcat: 我在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)

Please help me what am I doing wrong here? 请帮帮我,我在这里做错了什么? why I am getting java.io.IOException: HTTP response code: 400 for URL exception? 为什么我得到java.io.IOException:HTTP响应代码:400的URL异常?

First of all, this is incorrect: 首先,这是不正确的:

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

It should be 它应该是

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

Your issue is on this line: 您的问题出在这一行:

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

You can't send a Content-Length of 0 when you have entity body appended in HTTP message. 在HTTP消息中附加实体主体时,不能发送Content-Length0

Rather, set it as 相反,将其设置为

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器返回URL的HTTP响应代码:400:java.io.IOException - Server returned HTTP response code: 400 for URL : java.io.IOException java.io.IOException:服务器返回HTTP响应代码:400表示URL - java.io.IOException: Server returned HTTP response code: 400 for URL 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器为URL返回HTTP响应代码:400 - java.io.IOException: Server returned HTTP response code: 400 for URL: HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器返回的HTTP响应代码:URL的400 - java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400 - server returns java.io.IOException: Server returned HTTP response code: 400 for URL 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code Spring 启动:返回 HTTP 响应代码 400,嵌套异常为 java.ZF98ED07A4D5F50F4F770Exception - Spring Boot: returned HTTP response code 400, nested exception is java.io.IOException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM