- /**
- * 中国银行支付网关---银行回调的接口
- * @svncode <a href="svn://10.210.71.10/sinapay_bank/src/java/cn/com/sina">svn://10.210.71.10/sinapay_bank/src/java/cn/com/sina
- * @package cn.com.sina.pay.Bank.BOC
- * @author yuchao1@staff.sina.com.cn
- * @date 20101014
- * @access limited by password
- * @reference cn.com.sina.pay.ICBC
- *
- */
- import java.io.*;
- import java.util.*;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.Namespace;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.xml.sax.InputSource;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.methods.*;
- /**
- * 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面
- * @author Liudong
- */
- public class HttpClient1{
- public static void main(String[] args) throws IOException
- {
- HttpClient client = new HttpClient();
- //设置代理服务器地址和端口
- //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
- //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https
- HttpMethod method = new GetMethod("<a href="http://localhost/test/boc.xml">http://localhost/test/boc.xml");
- //使用POST方法
- //HttpMethod method = new PostMethod("<a href="http://java.sun.com">http://java.sun.com";);
- client.executeMethod(method);
- //打印服务器返回的状态
- System.out.println(method.getStatusLine());
- //打印返回的信息
- String resCode = method.getResponseBodyAsString();
- System.out.println("the is my other xml:"+resCode);
- //释放连接
- method.releaseConnection();
- StringReader read = new StringReader(resCode);
- InputSource source = new InputSource(read);
- SAXBuilder sb = new SAXBuilder();
- try{
- Document doc = sb.build(source);
- Element root = doc.getRootElement();
- System.out.println("the getName is:"+root.getName());
- List jiedian = root.getChildren();
- //获得XML中的命名空间(XML中未定义可不写)
- Namespace ns = root.getNamespace();
- Element et = null;
- List orderList = null;
- for (int i=0;i<jiedian.size();i++)
- {
- et = (Element)jiedian.get(i);
- if(et.getName().equals("header")){
- System.out.println(et.getChild("merchantNo", ns).getText());
- }
- if(et.getName().equals("body")){
- orderList = et.getChildren();
- System.out.println("body is :");
- System.out.println(et.getChild("orderTrans", ns).getChild("orderStatus").getText());
- }
- }
- for (int i=0;i<orderList.size();i++)
- {
- et = (Element)orderList.get(i);
- if(et.getName().equals("orderTrans")){
- System.out.println(et.getChild("payTime", ns).getText());
- }
- }
- }catch(JDOMException e){
- e.printStackTrace();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/250320132551.html
来源: http://www.codesnippet.cn/detail/250320132551.html