码迷,mamicode.com
首页 > Web开发 > 详细

http请求

时间:2015-09-08 00:34:51      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:



/**
	 * 直接发送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请求

标签:

原文地址:http://my.oschina.net/zhongwenhao/blog/502545

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!