50,526
社区成员
发帖
与我相关
我的任务
分享
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)
刚看到你那个异常信息,它提示io异常啊? wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
wordspace编码格式,数据库编码格式,html 编码格式,各种最好弄成统一的,错误信息呢?贴出来看看
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("马二");
}
}
server 服务器的编码格式修改了么? server.xm里也需要修改 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>
用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();
}
}
;
编码还哪里能改呀,我加了这两个还是不对有乱码了吧,在页面收不到这个字符串,看看字符集对不对,
有乱码了吧,在页面收不到这个字符串,看看字符集对不对,