【问题标题】:Getting POST response in a Get HttpURLConnection request在 Get HttpURLConnection 请求中获取 POST 响应
【发布时间】:2015-10-23 21:23:07
【问题描述】:

我不知道为什么,但是当我执行 GET 请求时收到 POST 响应,这是我的方法:

public String performGetBrandCall(String requestURL) {

    URL url;
    String response = "";
    try {
        url = new URL(requestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Bearer " + MainActivity.TOKEN);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode=conn.getResponseCode();
        Log.d(TAG, "Response Code: " + responseCode);


        if (responseCode == HttpsURLConnection.HTTP_OK || responseCode == HttpsURLConnection.HTTP_CREATED) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        } else {
            if (responseCode >= 400 && responseCode < 500){
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            } else {
                response="";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
} 

我正在使用 setRequestProperty 添加标头,这是我为请求设置的唯一“参数”,但我收到了发布响应,但我不知道为什么。有什么建议吗?

【问题讨论】:

    标签: android post get httpurlconnection


    【解决方案1】:

    根据 Android 开发者指南:

    SetDoOutput 设置指示此 URLConnection 是否允许输出的标志。建立连接后无法设置。

    仅当您使用 POST 请求时才必须调用此方法...如果您正在执行 GET 请求并且 SetDoOutput 为 true,则您的请求将是 POST 请求。

    我只删除了那行,现在我得到了我的 GET 响应:

    public String performGetCall(String requestURL) {
    
        URL url;
        String response = "";
        try {
            url = new URL(requestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Bearer "+ MainActivity.TOKEN);
            conn.setDoInput(true);
    
            int responseCode=conn.getResponseCode();
            Log.d(TAG, "Response Code: "+responseCode);
    
            if (responseCode == HttpsURLConnection.HTTP_OK || responseCode == HttpsURLConnection.HTTP_CREATED) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            } else {
                if (responseCode >= 400 && responseCode < 500){
                    String line;
                    BufferedReader br=new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                    while ((line=br.readLine()) != null) {
                        response+=line;
                    }
                } else {
                    response="";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return response;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多