发送json格式,有中文就会报400错误是怎么改 纯英文正常

eagllle 2016-08-26 09:13:27
protected String ADD_URL;
public String appadd (String jsonStr) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
StringBuffer sb = new StringBuffer("");
try {
//创建连接
URL url = new URL(ADD_URL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");//向服务器发送post请求
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);

//请求头
connection.setRequestProperty("Content-Type",
"application/json");//;charset=GBK
// connection.setRequestProperty(JwtUtils.HEADER_SIGN,JwtUtils.sign(null));
connection.connect();

//POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.write(jsonStr.toString().getBytes());
// out.writeBytes(jsonStr);
if(out==null){
StringTools.printError("数据写入失败");
}
out.flush();//清空缓冲区的数据流
out.close();

//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes("GBK"), "GBK");
sb.append(lines);
}
if(sb==null){
StringTools.printError("读取数据失败");
}
reader.close();
// 断开连接
connection.disconnect();
}catch(ConnectException e){
StringTools.printError("连接超时");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( sb.toString());
return sb.toString();

}

java.io.IOException: Server returned HTTP response code: 400 for URL:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
at com.ufgov.AccFiles.service.PostUrlTools.appadd(PostUrlTools.java:54)
at com.ufgov.AccFiles.service.AccFilesOperService.userSync(AccFilesOperService.java:30)
at com.ufgov.AccFiles.service.AccFilesOperService.main(AccFilesOperService.java:45)
...全文
729 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
君宏 2018-06-29
  • 打赏
  • 举报
回复
将DataOutputStream out = new DataOutputStream(connection.getOutputStream());改成
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
小羊哎CC 2018-01-29
  • 打赏
  • 举报
回复
//POST请求 DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write(jsonStr.toString().getBytes()); -----------------------------------改成下面这样------------------------------- //POST请求 DataOutputStream out = new DataOutputStream( connection.getOutputStream()); out.write( jsonStr.getBytes("utf-8"));
拘谨的小人 2016-08-29
  • 打赏
  • 举报
回复
设置请求头 编码格式
eagllle 2016-08-29
  • 打赏
  • 举报
回复
eagllle 2016-08-28
  • 打赏
  • 举报
回复
引用 12 楼 u012404135 的回复:
引用 11楼GGHidezhang 的回复:
引用 10楼GGHidezhang 的回复:
wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
刚看到你那个异常信息,它提示io异常啊?
看看你请求的url中有没有空格
url应该没问题,因为英文格式就能发送过去,中文就不行
GGHidezhang 2016-08-26
  • 打赏
  • 举报
回复
引用 11楼GGHidezhang 的回复:
引用 10楼GGHidezhang 的回复:
wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
刚看到你那个异常信息,它提示io异常啊?
看看你请求的url中有没有空格
GGHidezhang 2016-08-26
  • 打赏
  • 举报
回复
引用 10楼GGHidezhang 的回复:
wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
刚看到你那个异常信息,它提示io异常啊?
GGHidezhang 2016-08-26
  • 打赏
  • 举报
回复
wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
eagllle 2016-08-26
  • 打赏
  • 举报
回复
顶顶顶
eagllle 2016-08-26
  • 打赏
  • 举报
回复
	public class AccFilesOperService {
	public String userDel(String user_name) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException{
		JSONObject object=new JSONObject();
		object.put("user_name", user_name);
		String jsonStr=object.toString();
		System.out.println(jsonStr);
		posturl.ADD_URL = config.AccFilesIp()
		return posturl.appadd(jsonStr);
	}
	public static void main(String[] args) throws  NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
		AccFilesOperService acc=new AccFilesOperService();
		acc.userDel("马二");
	}
}
eagllle 2016-08-26
  • 打赏
  • 举报
回复
程序走到第34行就会报错了
eagllle 2016-08-26
  • 打赏
  • 举报
回复
引用 8 楼 qq_35124119 的回复:
server 服务器的编码格式修改了么? server.xm里也需要修改 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>
也是Encoding="UTF-8"呀
蓝莲花v587 2016-08-26
  • 打赏
  • 举报
回复
server 服务器的编码格式修改了么? server.xm里也需要修改 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>
eagllle 2016-08-26
  • 打赏
  • 举报
回复
引用 6 楼 u012404135 的回复:
引用 4楼GGHidezhang 的回复:
有乱码了吧,在页面收不到这个字符串,看看字符集对不对,
用UTF-8 试试
 public class PostUrlTools{
	protected String ADD_URL;
    public  String appadd (String jsonStr) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    	 StringBuffer sb = new StringBuffer("");
        try {
        	//创建连接
            URL url = new URL(ADD_URL);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);// 使用 URL 连接进行输出 
            connection.setDoInput(true);// 使用 URL 连接进行输入
            connection.setRequestMethod("POST");//向服务器发送post请求
            connection.setUseCaches(false);// 忽略缓存  
            connection.setInstanceFollowRedirects(true);
            
            //请求头
          connection.setRequestProperty("Content-Type",
                    "application/json;charset=utf-8");
//            connection.setRequestProperty(JwtUtils.HEADER_SIGN,JwtUtils.sign(null));
            connection.connect();

            //POST请求
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
           out.writeBytes(jsonStr);
            if(out==null){
           StringTools.printError("数据写入失败");
            }
            out.flush();//清空缓冲区的数据流
            out.close();

            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"UTF-8"));
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes());
                sb.append(lines);
            }  if(sb==null){
            	StringTools.printError("读取数据失败");
            }
          /*  int content=0;
            while((content=reader.read())!=-1){
             	 sb.append((char)content);
             }*/
          
            reader.close();
            // 断开连接
            connection.disconnect();
        }catch(ConnectException e){
        	StringTools.printError("连接超时");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println( sb.toString());
      

		return sb.toString();

    }


}
;
编码还哪里能改呀,我加了这两个还是不对
GGHidezhang 2016-08-26
  • 打赏
  • 举报
回复
引用 4楼GGHidezhang 的回复:
有乱码了吧,在页面收不到这个字符串,看看字符集对不对,
用UTF-8 试试
nikyotensai 2016-08-26
  • 打赏
  • 举报
回复
引用 4 楼 u012404135 的回复:
有乱码了吧,在页面收不到这个字符串,看看字符集对不对,
+1
GGHidezhang 2016-08-26
  • 打赏
  • 举报
回复
有乱码了吧,在页面收不到这个字符串,看看字符集对不对,

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧