簡體   English   中英

HttpsURLConnection頻繁收到504錯誤

[英]HttpsURLConnection getting 504 error frequently

我在Java-1.8中使用HttpsURLconnection並將其發布到具有某些參數的URL。 很多時候獲得200成功響應,但有些時候則獲得504網關超時錯誤。

Iam長時間在此錯誤中苦苦掙扎。 任何指導將大有幫助。 下面是正在使用的代碼。

public static void sendPostRequest(String url, ResourceRequest resourceRequest,
        ResourceResponse resourceResponse)
{ 
    String token_response = "";

                String grant_password = PrefsPropsUtil.getString("grant.type");
                String client_id = PrefsPropsUtil.getString("client.id");
                String user_name = PrefsPropsUtil.getString("user.name");
                String client_secret = PrefsPropsUtil.getString("client.secret");
                PrintWriter out = resourceResponse.getWriter();
                URL obj = new URL(url);

                System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
                System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
               //add request header
                con.setRequestMethod("POST");
              //  con.setRequestProperty("User-Agent", USER_AGENT);
                con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
                String urlParameters = "grant_type=" + grant_password + "&client_id=" + client_id + "&username=&client_secret=" + client_secret;
                // Send post request
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();
                int responseCode = con.getResponseCode();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                con.disconnect();
                //print result
                System.out.println(response.toString());
                out.println(response);

}

在上面的代碼中,我不知道我在哪里做錯了,請對此進行糾正。

5xx代碼被視為服務器錯誤。 因此,這很可能不是請求方的問題。

504網關超時:服務器充當網關或代理,沒有及時收到上游服務器的響應。

我建議您檢查服務器的代理設置是否正確或上游服務器的穩定性等HttpsURLconnection並不能說太多。

暫無
暫無

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

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