zoukankan      html  css  js  c++  java
  • 关于java.io.IOException: Server returned HTTP response code: 400 for URL报错和string.getBytes()字符集

    400 请求出错:由于语法格式有误,服务器无法理解此请求
    总论:这种错误应该会有很多原因,这里指出的是因为字符集编码的原因导致400,主要代码:向服务器发送请求传输json参数用的是out.write(json.getBytes())(读取的是操作系统的字符集,如果操作系统与部署项目的服务器不同则报错);改为out.writeChars(json);或out.write(json.getBytes(服务器编码))即可。如下代码16行

     1             //创建连接             
     2             URL url = new URL(u);
     3             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     4             connection.setDoOutput(true);
     5             connection.setDoInput(true);
     6             connection.setRequestMethod("POST");
     7             connection.setRequestProperty("connection", "keep-alive");
     8             connection.setConnectTimeout(30000);
     9             connection.setReadTimeout(30000);
    10             connection.setUseCaches(false);
    11             connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    12             connection.connect();
    13             // POST请求 
    14              out = new DataOutputStream(connection.getOutputStream());
    15             String json = message.toString();
    16             out.writeChars(json); // 这行是关键我之前写的是 out.write(json.getBytes());
    17             System.out.println(json);
    18             out.flush();
    19             // 读取响应 
    20             reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    21             String lines;
    22             StringBuffer sb = new StringBuffer("");
    23             while ((lines = reader.readLine()) != null) {
    24                 lines = new String(lines.getBytes(), "utf-8");
    25                 sb.append(lines);
    26             }
    27             JSONObject jsStr =JSONObject.fromObject(sb.toString());
    28             //获取响应值,判断是否验证通过
    29             String code = (String) jsStr.get("code");
    30             String msg=(String) jsStr.get("msg");
    31             System.out.println("code:"+code+",msg:"+msg);
    32             //接口返回验证数据是否通过
    33                 if("0".equals(code)){
    34                     result = "success";
    35                 } else{
    36                     result = "fail";
    37                     System.out.println("下发出错:错误原因为" + msg + "下发内容为:" + json);
    38                 }        
    39             reader.close();
    40             // 断开连接 
    41             connection.disconnect(); 
    42             

    这其中也经过一些波折,比如在自己的eclipse上运行正常,部署到服务器上就不行了,json数据是一样的但是因为自己本地的服务器和部署项目的服务器编码不同,而产生这种错误。

    在解决问题时百度做了很久,因为不适应我的项目做了一些错误的更改,比如找空格,在我的json中是存在空格的,但是我用的是post提交,并不会有影响。还有说要双引号转成单引号的,这让原本本地不报错的程序也报错了,可是我还傻傻的开心以为是引号的事。还有让转json编码的,但是无论json转成什么格编码out.write(json.getBytes()) 传输的时候编码格式没有设定,还是去默认操作系统的编码还是不会改对

  • 相关阅读:
    GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名(转载)
    vector,list,deque容器的迭代器简单介绍
    自己动手实现简单的Vector
    浅析STL allocator
    STL中的Traits编程技法
    模板类的全特化、偏特化
    自己动手实现智能指针auto_ptr
    各种排序算法的总结和比较(转)
    (CVE-2017-7269 ) IIS6.0实现远程控制
    (CVE-2016-5195)脏牛本地提权
  • 原文地址:https://www.cnblogs.com/sunshineweb/p/7772222.html
Copyright © 2011-2022 走看看