簡體   English   中英

java.io.IOException:HTTP響應代碼:400用於URL

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

這是我的示例代碼:

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響應代碼:400的URL異常?

首先,這是不正確的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM