java 发送HTTP POST 出现500错误

cooc2000 cooc2000 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
    public String Post(String content, String charset, String url) {
try {
URL u = new URL(url);
HttpURLConnection http = (HttpURLConnection) u.openConnection();

/*
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
http.setConnectTimeout(50000);
http.setReadTimeout(50000);
http.connect();

OutputStream output = http.getOutputStream();
output.write(content.getBytes());
output.flush();
output.close();
*/

//如果吧注释取消掉就出现了严重: null
java.io.IOException: Server returned HTTP response code: 500 for URL: 问题出在哪呢


System.out.println(http.getResponseCode());
StringBuilder sb = new StringBuilder();
BufferedReader in;
if (charset != null) {
in = new BufferedReader(new InputStreamReader(http.getInputStream(), charset));
} else {
in = new BufferedReader(new InputStreamReader(http.getInputStream()));
}

String line = "";
while ((line = in.readLine()) != null) {
sb.append(line).append("\n");
}
in.close();
http.disconnect();
return sb.toString();
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(url + " Post ");
return null;
}
}
...全文
评论 13
收藏 已收藏
分享
返回顶部
cooc2000
Java SE 62,621 成员 307,251 内容