记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx
踩坑经历
因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTTP response code: 500 for URL。
当时情况是本地测试通过,正常返回,放到linux云服务器上测试通过,正常返回,放到windows server服务器上就有问题了,就是上面所说的。
根据报错分析首先联系接收方,发现对方没有报错内容,于是从自身找问题。首先想到是编码格式于是
尝试1
参考:https://blog.csdn.net/heweirun_2014/article/details/45535193
connection.setRequestProperty(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);
最终没有解决问题
尝试2
conn.setRequestProperty("charsert", "utf-8");
最终没有解决问题
尝试3
参考:https://blog.csdn.net/maggiehexu/article/details/6448347
排除掉请求参数为空
最终没有解决问题
尝试4
out.println(param.getBytes("UTF-8"));
最终没有解决问题
尝试5
使用
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(param);
替换的
PrintWriter out = new PrintWriter(conn.getOutputStream()); // 用PrintWriter进行包装
out.println(param);
问题解决了!!!!!!!!一万头草泥马奔腾而过>_<
最后贴上代码希望对你有所帮助
/** post请求 */
public static String reqPost(String url, String param) throws IOException {
String res = "";
URLConnection conn = getConnection(url); // POST要求URL中不包含请求参数
conn.setDoOutput(true); // 必须设置这两个请求属性为true,就表示默认使用POST发送
conn.setDoInput(true);
//conn.setRequestProperty("charsert", "utf-8");
// 请求参数必须使用conn获取的OutputStream输出到请求体参数
// 用PrintWriter进行包装
/*PrintWriter out = new PrintWriter(conn.getOutputStream());
out.println(param.getBytes("UTF-8"));*/
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(param);
out.flush(); // 立即充刷至请求体)PrintWriter默认先写在内存缓存中
try// 发送正常的请求(获取资源)
{
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
res += line + "\n";
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.toString());
}
return res;
}
记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx的更多相关文章
- 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法
java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...
- java.io.IOException: Server returned HTTP response code: 411 for URL
今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...
- java get请求带参数报错 java.io.IOException: Server returned HTTP response code: 400 for URL
解决方案 在使用JAVA发起http请求的时候,经常会遇到这个错误,我们copy请求地址在浏览器中运行的时候又是正常运行的,造成这个错误的原因主要是因为请求的URL中包含空格,这个时候我们要使用URL ...
- bug日记之-------java.io.IOException: Server returned HTTP response code: 400 for URL
报的错误 出事代码 出事原因 解决方案 总结 多看源码, 我上面的实现方式并不好, 如果返回的响应编码为400以下却又不是200的情况下getErrorStream会返回null, 所以具体完美的解决 ...
- 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”;Syntax error on token "Invalid Character";Server returned HTTP response code: 503 for URL;
元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”:复制的代码有中文空格 Syntax error on token "Invalid Character&qu ...
- 异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null
nutch 运行时异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null 参考 ...
- Java Server returned HTTP response code: 401
今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...
- 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误
一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...
- android环境下解决java.io.IOException: Malformed ipv6异常的方法
今天做客户端想服务端提交信息的时候,报出了如标题所显示的方法 方法以及参数如下: 输入的参数为:http://192.168.1.173:8080/Api/petinfo/petinfo?flag=a ...
随机推荐
- uploadify的使用错误
在看singwa的视频教程中,学习使用hui-admin模版,在使用uploadify插件上传图片中出现错误. ReferenceError: Can't find variable: $因为使用JQ ...
- 【网络是怎么连接的】一、浏览器与HTTP协议
浏览器: 1.生成HTTP消息: 1).网址结构: http://user:password@www.glasscom.com:80/dir/file1.htm 2).HTTP服务基本思路: a).请 ...
- 2019 ACM-ICPC 南京 现场赛 H. Prince and Princess
题意 王子想要娶公主,但是需要完成一个挑战:在一些房间中找出公主在哪. 每个房间有一个人,他们彼此知道谁在哪个房间.可以问他们三种问题: 你是谁? 在某个房间是谁? 公主在哪个房间? 有三类人,一类一 ...
- PAT_A1100#Mars Numbers
Source: PAT A1100 Mars Numbers (20 分) Description: People on Mars count their numbers with base 13: ...
- docker-compose的scale的用法
用到的三个文件 docker-compose version: " services: redis: image: redis web: build: context: . dockerfi ...
- python获取Linux发行版名称
我必须从Python脚本中获取Linux发行版名称.dist平台模块中有一个方法: import platform platform.dist() 但在我的Arch Linux下它返回: >&g ...
- Draggable(拖动框)
一.class加载方式 <div id="box" class="easyui-draggable" style="width:400px;he ...
- 在egg中配置cors
在egg中配置 cors(跨域) 第一步,安装 egg-cors npm i egg-cors --save 第二步,配置 cors 在config/plugin.js文件下添加: exports.c ...
- postgresql 数据库的备份和还原
第一步:通过 cmd 进入到postgresql 安装目录的 bin 下: windows : cd C:\PostgreSQL\pg95\bin ubuntu : cd /etc/postgresq ...
- Codeforces 1156D 带权并查集
题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案 ...