httpclient4

/**
002  * 中国银行支付网关---银行回调的接口
003  * @svncode svn://10.210.71.10/sinapay_bank/src/java/cn/com/sina
004  * @package cn.com.sina.pay.Bank.BOC
005  * @author yuchao1@staff.sina.com.cn
006  * @date 20101014
007  * @access limited by password
008  * @reference cn.com.sina.pay.ICBC
009  *
010  */
011 import java.io.*;
012 import java.util.*;
013 import org.jdom.Document;
014 import org.jdom.Element;
015 import org.jdom.Namespace;
016 import org.jdom.JDOMException;
017 import org.jdom.input.SAXBuilder;
018 import org.xml.sax.InputSource;
019  
020 import org.apache.commons.httpclient.*;
021 import org.apache.commons.httpclient.methods.*;
022 import org.apache.commons.httpclient.methods.PostMethod;
023 import org.apache.commons.httpclient.params.HttpMethodParams;
024 /**
025  * 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面
026  * @author yuchao
027  */
028 public class HttpClient1{
029     public static void main(String[] args) throws IOException
030     {
031         String merchantNo = "104110053004253";
032         String orderNo = "101023416806";
033         String signData = "SDJFALSF";
034          
035         HttpClient client = new HttpClient();
036              
037             //使用POST方法
038             PostMethod postMethod = newPostMethod("https://ebspay.boc.cn/PGWPortal/QueryOrder.do");
039             /**
040              * 使用POST方式提交数据
041              */
042           NameValuePair[] orderInfo = {newNameValuePair("merchantNo",merchantNo),new NameValuePair("orderNos",orderNo),
043                   new NameValuePair("signData",signData),};
044           postMethod.setRequestBody(orderInfo);
045  
046           client.executeMethod(postMethod);
047            
048           int code = postMethod.getStatusCode(); 
049           if (code == HttpStatus.SC_OK){
050             String info = null;
051             info = new String(postMethod.getResponseBodyAsString());
052           }
053            
054           /**
055            * 打印服务器返回的状态
056            */
057           System.out.println("the post return value"+postMethod.getStatusLine());
058           /**
059            * 打印结果页面
060            */
061           String response =   newString(postMethod.getResponseBodyAsString().getBytes("UTF-8"));
062            /**
063             * 打印返回的信息
064             */
065           System.out.println("the getBytes() xml is:"+response);
066            //打印返回的信息
067          String resCode = postMethod.getResponseBodyAsString();
068          System.out.println("the is my other xml:"+resCode);
069            //释放连接
070          postMethod.releaseConnection();
071          
072       
073       
074      StringReader read = new StringReader(resCode);
075      InputSource source = new InputSource(read);
076      SAXBuilder sb = new SAXBuilder();
077       
078      try{
079         Document doc = sb.build(source);
080         Element root = doc.getRootElement();
081          
082         System.out.println("the getName is:"+root.getName());
083         List jiedian = root.getChildren();
084         //获得XML中的命名空间(XML中未定义可不写)
085         Namespace ns = root.getNamespace();
086         Element et = null;
087         List orderList = null;
088          
089         for (int i=0;i<jiedian.size();i++)
090         {
091             et = (Element)jiedian.get(i);
092              
093             if(et.getName().equals("header")){
094                 System.out.println(et.getChild("merchantNo", ns).getText());
095                 System.out.println(et.getChild("exception", ns).getText());
096             }
097              
098             if(et.getName().equals("body")){
099                 orderList = et.getChildren();
100             System.out.println(et.getChild("orderTrans", ns).getChild("orderStatus").getText());
101             }
102              
103         }
104         for (int i=0;i<orderList.size();i++)
105         {
106             et = (Element)orderList.get(i);
107              
108             if(et.getName().equals("orderTrans")){
109                 System.out.println(et.getChild("payTime", ns).getText());              
110             }
111              
112         }
113         }catch(JDOMException e){
114             e.printStackTrace();
115       }catch(IOException e){
116             e.printStackTrace();
117         }
118    }
119 }

你可能感兴趣的:(httpclient)