标签:
/** * 直接发送HTTP请求 * * @param message * 上送报文 * @return 返回的报文 */ public String sendHttpRequest(String data) { StringBuffer sb = new StringBuffer(""); HttpURLConnection connection = null; BufferedInputStream in = null; BufferedOutputStream o = null; try { System.out.println("Http 上送银行数据为:"+data); System.out.println("Http URL为:"+"http://ip:8002/corporbank/httpAccess"); connection = (HttpURLConnection) new URL("http://ip:8002/corporbank/httpAccess").openConnection(); connection.setRequestProperty("content-type", "text/xml; charset="+ "UTF-8"); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); o = new BufferedOutputStream(connection.getOutputStream()); o.write(data.getBytes("UTF-8")); o.flush();// in = new BufferedInputStream(connection.getInputStream()); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { sb.append(new String(buffer, 0, len, "UTF-8")); } } catch (Exception e) { sb = new StringBuffer("-1"); e.printStackTrace(); } finally { if (o != null) { try { o.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (in != null) { try { in.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (connection != null) { try { connection.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } } System.out.println("http通讯银行端返回的交易数据:"+sb.toString()); return sb.toString(); }
标签:
原文地址:http://my.oschina.net/zhongwenhao/blog/502545