java.io.IOException: Server returned HTTP response code: 400 for URL: http://xxxx

初识篇

1.0介绍

1.1自我介绍

大家好,我是IT摆烂工程师,今天正式在博客上发布文章,请大家多多支持!!!

1.2写csdn博客初衷

原因是记录一些关于java的编程知识,在工作中的一些困难点,博主也是编程菜鸟,希望在探索的过程中一步步变“秃”,最重要的是记录自己的成长历程。

接下来就开始本篇博客的主要内容了

2.0对接接口

2.1java.io.IOException: Server returned HTTP response code: 400 for URL: http://xxxx

以上错误是对接接口常见的错误,是怎么形成的呢,菜鸟博主也是不太会,所以记录一下。

2.2解决

可能出现错误的原因

  1. 可能是没有做字符转义
  2. 可能是没有了解完全第三方接口
  3. ……..

2.2.1第一种错误,解决代码如下

UrlUtil.sendGetHttpUrlUrlUtil.sendGetHttpUrlURLEncoder.encode("张三 =","UTF-8")    //调用端
    @GetMapping("/doGetLogin")
    public Map<String, Object> doGetLogin() throws IOException {
        System.out.println("调用方");
        String urlstr="name=张三 =&pwd=123456=";  //请注意张三后面有一个空格
        String newUrl="http://127.0.0.1:7777/user/login"+"?"+urlstr;
        String str = UrlUtil.sendGetHttpUrl(newUrl,"token"); //工具包网上有
        Map map = JSONObject.parseObject(str, Map.class); //引入fastjson依赖
        return map;
    }
    @GetMapping("/doGetLogin2")
    public Map<String, Object> doGetLogin2() throws IOException {
        System.out.println("调用方");
        String urlstr ="name="+ URLEncoder.encode("张三 =","UTF-8")+"&pwd="+URLEncoder.encode("123456=","UTF-8");
        String newUrl="http://127.0.0.1:7777/user/login"+"?"+urlstr;
        String str = UrlUtil.sendGetHttpUrl(newUrl,"token");
        Map map = JSONObject.parseObject(str, Map.class);
        return map;
    }
    
    //第三方接口端
    @GetMapping("/login")
    public R login(@RequestParam(required = false) Map<String, Object> map, HttpServletRequest request) throws UnsupportedEncodingException {
        System.out.println("模拟第三方接口");
        System.out.println(map.toString());
        System.out.println(request.getQueryString());
        System.out.println(request.getHeader("Authorization")); //获取token
        if (map.get("name") != null && map.get("pwd") !=null) {
            if (map.get("name").equals("张三 =") && map.get("pwd").equals("123456=")) {
                return R.ok();
            }
            return R.error("账号和密码输入不正确");
        }else {
            return R.error("账号和密码不能为空");
        }
    }

注意:

  1. 使用第一种方法会报错,因为有空格字符,没有进行转义,所以会报错误

    java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:7777/user/login?name=张三 =&pwd=123456=
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0]
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0]
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0]
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0]
    	at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1888) ~[na:1.8.0]
    	at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1883) ~[na:1.8.0]
    	at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0]
    	at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1882) ~[na:1.8.0]
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1455) ~[na:1.8.0]
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) ~[na:1.8.0]

    最主要的是name=张三 =&pwd=123456=,没有进行转义

  2. 使用第二种方式进行调用就不会出现错误

    name=%E5%BC%A0%E4%B8%89+%3D&pwd=123456%3D
    原因是张三 =转义成%E5%BC%A0%E4%B8%89+%3D这个字符串了,可以进行http请求调用 空格转为+号了,最主要的空格出现在url中会破坏这个http请求路径,所有要进行转义。

  3. URLEncoder.encode(“张三 =”,”UTF-8″) –java.net.URLEncoder

    可以进行对”张三 =”这个字符串转义成 %E5%BC%A0%E4%B8%89+%3D
  4. 服务器应该是给url做了处理,所以不需要自己手动进行url路径转义回来,但也可以手动进行转义

    System.out.println(URLDecoder.decode(“转义过后的字符串(%E5%BC%A0%E4%B8%89+%3D)”, “UTF-8”));

    可能有工具类封装了,但是博主没找^_^

  5. 最后附上我的–UrlUtil.sendGetHttpUrl

    /**
     * get请求
     * @param url 请求地址
     * @param token 请求头携带的token
     * @return 返回第三方接口的返回信息(一般是code,data等数据)
     * @throws IOException
     */
    public static String sendGetHttpUrl(String url,String token) throws IOException {
        URL realUrl= new URL(url);
        URLConnection connection=realUrl.openConnection();
        //设置请求超时
        connection.setConnectTimeout(6000);
        //设置通用的请求属性头
        connection.setRequestProperty("accept","*/*");
        connection.setRequestProperty("connection","Keep-Alive");
        connection.setRequestProperty("user-agent","Mozilla/4.0(compatiable;MSIE 6.0; Windows NT 5.1; sv1)");
        if (token != null) {
            connection.setRequestProperty("Authorization",token);
        }
        connection.connect();
        //设置响应超时
        connection.setReadTimeout(6000);
        //获取所有响应头字段
        Map<String, List<String>> map= connection.getHeaderFields();
        for(String key :map.keySet()){

        }
        //定义bufferedReader字符流读取URL的响应
        BufferedReader bred=new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line="";
        String str="";
        while((line=bred.readLine()) !=null){
            str+=line;
        }
        System.out.println(str);
        //关闭 流
        bred.close();
        return str;
    }

2.2.2第二种错误–不了解第三方接口

  1. 这个博主就无能为力了

    只能说是多看多学多写多问

  2. 如果是官方的接口,可以去开放平台找接口文档

2.2.3其他的就不知道咯,如果有也可以留言,一起研究共同进步!!

最后在这里谢谢大家的浏览。祝大家工作顺利,准时下班!!!!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年12月7日
下一篇 2023年12月7日

相关推荐