记录一次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);

 

记录一次Http Get请求遇到的坑,请求返回Server returned HTTP response code: 400 for URL

上一篇:umijs 开发优化和生产优化


下一篇:往harbor上传镜像