已解決:記錄一次Http Get請求遇到的坑,請求返回Server returned HTTP response code: 400 for URL


public static String doGet(String url){
        StringBuffer buffer = new StringBuffer();
        try {
            URL getUrl = new URL(url);
            HttpURLConnection httpUrlConn = (HttpURLConnection) getUrl.openConnection();
            httpUrlConn.setDoOutput(false);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 將返回的輸入流轉換成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 釋放資源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

以上是HTTP GET請求的發送方法,代碼是沒有問題,而且傳遞來的參數在瀏覽器和postman都測試過,也是沒有問題,但是在代碼執行http請求的時候會返回“Server returned HTTP response code: 400 for URL”異常信息,具體的原因是訪問URL攜帶的參數里包含空格。

我的請求URL大概是這樣子的:

String methodApi = "token.create";
String param = paramJson.toJSONString();
String timestamp = sdf.format(new Date());
String v = "2";
String url = "https://xxxxxx.xxx/token/create";

StringBuffer urlBuffer = new StringBuffer();
urlBuffer.append(url).append("?")
            .append("app_key=").append(appkey).append("&")
            .append("grant_type=authorization_code&")
            .append("method=").append(methodApi).append("&")                   
            .append("param_json=").append(paramJson).append("&")
            .append("sign=").append(md5S).append("&")                   
            .append("timestamp=").append("timestamp").append("&")
            .append("v=").append(v);

大概就是:https://xxxxx.xxx/xxx/xxx?xxx=xxx&xxx=xxx&xxx=xxx這種

然后因為中間有個時間戳的參數 timestamp 數據是“2021-7-19 18:08:34”這種格式的日期,中間有空格,所以導致HTTP請求報錯。

解決方法為給有空格的請求參數轉碼使用:URLEncoder.encode("str","charset");方法

String methodApi = "token.create";
String param = paramJson.toJSONString();
String timestamp = sdf.format(new Date());
String v = "2";
String url = "https://xxxxxx.xxx/token/create";

StringBuffer urlBuffer = new StringBuffer();
urlBuffer.append(url).append("?")
            .append("app_key=").append(appkey).append("&")
            .append("grant_type=authorization_code&")
            .append("method=").append(methodApi).append("&")                   
            .append("param_json=").append(paramJson).append("&")
            .append("sign=").append(md5S).append("&")                   
            .append("timestamp=")
            .append(URLEncoder.encode("timestamp","utf-8"))
            .append("&") .append("v=").append(v);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM