1. 程式人生 > >介面中資料流的格式傳送請求和解析流請求

介面中資料流的格式傳送請求和解析流請求

資料流格式傳送請求:

HttpClient hc = new HttpClient();

            int httpcode = 0;
            hc.setConnectionTimeout(10000);
            hc.setTimeout(8000);
            PostMethod pm = new PostMethod(supplierServerUrl);
            String sign = util.MD5HEX(AgentID+Timestamp+AgentOrderID+GoodsTypeID+GoodsID+PayNumber+Amount+key);
            String source="{\"header\": {\"AgentID\":\""+AgentID+"\",\"Timestamp\":\""+Timestamp+"\",\"sign\":\""+sign+"\"},\"body\": {\"AgentOrderID\": \""+AgentOrderID+"\",\"GoodsTypeID\": \""+GoodsTypeID+"\",\"GoodsID\": \""+GoodsID+"\",\"PayNumber\": \""+PayNumber+"\",\"Amount\": \""+Amount+"\"}}";
            System.out.println(source);
            RequestEntity se = new StringRequestEntity(source,
                    "application/x-www-form-urlencoded","utf-8");
            pm.setRequestEntity(se);
            httpcode = hc.executeMethod(pm);
            responseStr=pm.getResponseBodyAsString();

            pm.releaseConnection();

解析流格式:

int httpcode = 0;
String resposeStr = "";
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(15000);
hc.setTimeout(15000);
PostMethod pm = new PostMethod(supplierServerUrl);
pm.setRequestBody(nvp);
httpcode = hc.executeMethod(pm);
InputStream is = pm.getResponseBodyAsStream(); 
            BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8")); 
            String line=null; 
            char[] c=new char[1024]; 
            int len=0; 
            while(-1 != (len = br.read(c))){ 
            line=new String(c, 0, len); 
            }
            responseStr=line;
pm.releaseConnection();
is.close();
br.close();