java.io.IOException: Server returned HTTP response code: 500 for URL解决办法
2015-08-10 17:14
956 查看
今天遇到一个问题,写了一个客户端远程查询数据,结果报错了:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://adserver.sinosns.cn/owner/owner_userAndOwner.action
客户端代码如下:
服务器端代码:
结果,因为传递的参数为空字符串,导致服务器端程序出现异常,没有数据返回客户端。所以在执行到
OutputStream os = con.getOutputStream();
这句代码的时候就报错了。
我的解决办法是在服务器端加上异常处理,并且在catch里面返回一个自定义的对象
这样就算发生异常,客户端也能去得到数据
java.io.IOException: Server returned HTTP response code: 500 for URL: http://adserver.sinosns.cn/owner/owner_userAndOwner.action
客户端代码如下:
/** * 执行一个HTTP POST请求,返回请求响应的HTML * * @param url请求的URL地址 * @param postData请求的查询参数 * @return 返回请求响应的HTML */ public static String doHttpPost(String url, String postData) { String data = null; try { URL dataUrl = new URL(url); HttpURLConnection con = (HttpURLConnection) dataUrl.openConnection(); // con.setRequestMethod("POST"); // con.setRequestProperty("Proxy-Connection", "Keep-Alive"); // con.setDoOutput(true); // con.setDoInput(true); con.setRequestMethod("POST"); con.setRequestProperty("Proxy-Connection", "Keep-Alive"); // con.setRequestProperty("ContentType","text/xml;charset=utf-8"); con.setReadTimeout(50000); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); OutputStream os = con.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.write(postData.getBytes("UTF-8")); dos.flush(); dos.close(); InputStream is = con.getInputStream(); DataInputStream dis = new DataInputStream(is); byte d[] = new byte[dis.available()]; dis.read(d); data = new String(d,"UTF-8"); // System.out.println(data); con.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } return data; }
服务器端代码:
CustomerModel customerModel = customerDao.getObjectByID(CustomerModel.class, Integer.valueOf(userid)); OwnerModel ownerModel = ownerDao.getObjectByID(OwnerModel.class, Integer.valueOf(ownerid)); if(null != customerModel){ object.put("userMac", customerModel.getMac()); object.put("userPhone",customerModel.getCustName()); }else { object.put("userMac", "0"); object.put("userPhone","0"); } if(null != ownerModel){ if(null==ownerModel.getIndustryId()){ object.put("industry", "0"); } else { object.put("industry", ownerModel.getIndustryId().toString()); } object.put("province", ownerModel.getProvinceId().toString()); object.put("city", ownerModel.getCityId().toString()); object.put("area", ownerModel.getAreaId().toString()); } else { object.put("industry", "0"); object.put("province", "0"); object.put("city", "0"); object.put("area", "0"); } return object.toString();
结果,因为传递的参数为空字符串,导致服务器端程序出现异常,没有数据返回客户端。所以在执行到
OutputStream os = con.getOutputStream();
这句代码的时候就报错了。
我的解决办法是在服务器端加上异常处理,并且在catch里面返回一个自定义的对象
这样就算发生异常,客户端也能去得到数据
相关文章推荐
- APP HTTP接口报文的加密与压缩
- (转载)卷积神经网络
- [Android基础]Android中使用HttpURLConnection
- Http协议与TCP协议理解
- HTTP调试工具推荐:Fiddler
- 《计算机网络》-----课本内容
- TCP/IP数据报格式分析
- nginx安全下载模块ngx_http_secure_link_module
- 黑马程序员-网络编程(一)
- Android 网络通信框架Volley简介(Google IO 2013)
- Java基础——网络编程(一)
- 卷积神经网络
- qperf测量网络带宽和延迟
- Http Session和Cookie
- 基于Java的socket网络通信
- 谈谈如何训练一个性能不错的深度神经网络
- 网络访问基础篇(适合新手)
- Android 异步加载数据 AsyncTask+Handler+AsyncHttpClient+Universal_Image_loader+Volley
- HTTP深入浅出 http请求
- WCF使用小结:(1)WCF接收HTTP POST数据的处理方法