关于java Servlet 连接某个服务器地址时出现的异常java.net.SocketException: Connection reset

woshilzha2 2015-12-03 03:00:22
我是第一次发帖,新人多多见谅。


本身是做Java 服务器的,很多东西用Servlet写,因为业务需要,像另外一个平台服务器进行获取某些信息,那个平台会返回我一个XML文件。


URL url = new URL("某个网址");
HttpsURLConnection urlcon = (HttpsURLConnection) url.openConnection();
urlcon.setRequestMethod("GET");//POST也试过
urlcon.connect();
InputStream in=urlcon.getInputStream();
fos = new FileOutputStream("../LoginXml/a.xml");
while ( (size = in.read(buf)) != -1)
{
fos.write(buf, 0, size);
}
fos.flush();
fos.close();



问题是只要程序一运行到urlcon.connect那里就报 Connection reset的异常,把这句去掉了,getinputStream那句也会报一样的异常。。。
由于是刚做开发的新人,很多东西不懂,希望有大神不吝赐教~
...全文
712 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshilzha 2015-12-04
  • 打赏
  • 举报
回复
自顶一发,希望有大神解答
woshilzha 2015-12-04
  • 打赏
  • 举报
回复
引用 2 楼 trocp 的回复:
PostMethod post = new PostMethod(url);
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		client.executeMethod(post);
		
		Header[] heads = post.getResponseHeaders();
		boolean isword = false;
		for(Header h : heads){
			String n = h.getName();
			if(n.equalsIgnoreCase("Content-Type")){
				String v = h.getValue();
				if(v.indexOf("application") >= 0 || v.indexOf("word") >= 0){
					//下载到的资源是图片
					isword = true;
					break;
				}
			}
		}
		
		boolean success = isword;
		System.out.println(" success :" + success);
		if(success){
			FileOutputStream out = new FileOutputStream(file);
			out.write(post.getResponseBody());
			out.close();
			return file;
		}else{
			return null;
		}
给你一个代码片段,目的是下载一个Word文档
也用了你的方法试了一下,运行到client.executeMethod(post)还是报connection reset的异常。。。。 感谢回复
woshilzha 2015-12-04
  • 打赏
  • 举报
回复
引用 6 楼 woshilzha 的回复:
[quote=引用 2 楼 trocp 的回复:]
PostMethod post = new PostMethod(url);
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		client.executeMethod(post);
		
		Header[] heads = post.getResponseHeaders();
		boolean isword = false;
		for(Header h : heads){
			String n = h.getName();
			if(n.equalsIgnoreCase("Content-Type")){
				String v = h.getValue();
				if(v.indexOf("application") >= 0 || v.indexOf("word") >= 0){
					//下载到的资源是图片
					isword = true;
					break;
				}
			}
		}
		
		boolean success = isword;
		System.out.println(" success :" + success);
		if(success){
			FileOutputStream out = new FileOutputStream(file);
			out.write(post.getResponseBody());
			out.close();
			return file;
		}else{
			return null;
		}
给你一个代码片段,目的是下载一个Word文档
也用了你的方法试了一下,运行到client.executeMethod(post)还是报connection reset的异常。。。。 感谢回复[/quote] 我是楼主
woshilzha2 2015-12-04
  • 打赏
  • 举报
回复
引用 4 楼 Tro_picana 的回复:
也可以考虑用Jsoup或是HttpClient去做,会容易点,一些方法都封装好的 Jsoup就一句话 String xmlString = Jsoup.conn(URL).ignortContentType(true).get().text();.//好像是这样,具体记不清了
尝试了你说的方法,一样报connect reset的错,我现在开始考虑是不是连接地址那边的问题了。 若是连接地址那边的问题的话,我直接把网址放在浏览器上却也能访问,太纳闷了。 不过谢谢你的解答
小白晒太阳 2015-12-03
  • 打赏
  • 举报
回复
也可以考虑用Jsoup或是HttpClient去做,会容易点,一些方法都封装好的 Jsoup就一句话 String xmlString = Jsoup.conn(URL).ignortContentType(true).get().text();.//好像是这样,具体记不清了
oO临时工Oo 2015-12-03
  • 打赏
  • 举报
回复
post.getResponseBodyAsString() 可以直接等到文本内容(就是你说的XML)
oO临时工Oo 2015-12-03
  • 打赏
  • 举报
回复
PostMethod post = new PostMethod(url);
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		client.executeMethod(post);
		
		Header[] heads = post.getResponseHeaders();
		boolean isword = false;
		for(Header h : heads){
			String n = h.getName();
			if(n.equalsIgnoreCase("Content-Type")){
				String v = h.getValue();
				if(v.indexOf("application") >= 0 || v.indexOf("word") >= 0){
					//下载到的资源是图片
					isword = true;
					break;
				}
			}
		}
		
		boolean success = isword;
		System.out.println(" success :" + success);
		if(success){
			FileOutputStream out = new FileOutputStream(file);
			out.write(post.getResponseBody());
			out.close();
			return file;
		}else{
			return null;
		}
给你一个代码片段,目的是下载一个Word文档
_南天北落 2015-12-03
  • 打赏
  • 举报
回复
整合axis2。很简单就会解决了。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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