DHL接口—数据交互

简单描述:按照DHL请求格式组装字符串,发送到服务器,返回特定格式的内容。

主要有两点:1、模版引擎freemarker(一般以ftl结尾;2、连接及发送请求到服务器并返回内容。

主要代码如下(这里主要列出下单模块的代码pickup_book):

pickup_book.ftl



    
        
            ${MessageTime}
            ${MessageReference}
            ${SiteID}
            ${Password}
        
    
    
        ${AccountType1}
        ${AccountNumber1}
        
            ${PersonName}
            ${Phone1}
            ${PhoneExtension}
        
    
    
        ${LocationType}
        ${CompanyName}
        ${Address1}
        ${Address2}
        ${PackageLocation}
        ${City}
        ${StateCode}
        ${DivisionName}
        ${CountryCode}
	${PostalCode}
    
    
        ${PickupDate}
        ${ReadyByTime}
        ${CloseTime}
        ${Pieces}
        
            ${Weight1}
            ${WeightUnit1}
        
    
    
        ${PersonName}
        ${Phone2}
        ${PhoneExtension}
    
    
        ${AccountType2}
        ${AccountNumber2}
        ${BillToAccountNumber}
        ${AWBNumber}
        ${NumberOfPieces}
        ${Weight2}
        ${WeightUnit2}
        ${GlobalProductCode}
        ${DoorTo}
        ${DimensionUnit}
        ${InsuredAmount}
        ${InsuredCurrencyCode}
        
            ${Weight3}
            ${Width}
            ${Height}
            ${Depth}
        
        ${SpecialService1}
        ${SpecialService2}
     
     
        D
        851624480
        100000000
        7520067111
        1
        1.0
        K
        D
        DD
        C
        999999.99
        USD
        
            1.0
            2
            2
            2
        
        S
        I
    

 

DHLClient.java

package com.hmnet.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class DHLClient {
    
    public static String getResXmlString (String clientRequestXML, String httpURL) throws IOException{
        try {
            String query = "isUTF8Support=true";
            URL servletURL = new URL(httpURL + "?" + query);
            HttpURLConnection servletConnection = null;
            servletConnection = (HttpURLConnection)servletURL.openConnection();
            servletConnection.setDoOutput(true);  // to allow us to write to the URL
            servletConnection.setDoInput(true);
            servletConnection.setUseCaches(false); 
            servletConnection.setRequestMethod("POST");

            servletConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");   
            servletConnection.setRequestProperty("Accept-Charset", "UTF-8");
            String len = Integer.toString(clientRequestXML.getBytes().length);
            servletConnection.setRequestProperty("Content-Length", len);

            servletConnection.connect();
            
            OutputStreamWriter wr = null;
            wr = new OutputStreamWriter(servletConnection.getOutputStream(), "UTF8");
            wr.write(clientRequestXML);
            wr.flush();
            wr.close();

            /*Code for getting and processing response from DHL's servlet*/
            InputStreamReader isr = null;
            isr = new InputStreamReader(servletConnection.getInputStream(),"UTF8");
            BufferedReader rd = new BufferedReader(isr);
            StringBuilder result = new StringBuilder();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line).append("\n");
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    private static String ftlUrl = "D:\\apache-tomcat-7.0.30\\webapps\\eTrade\\resource\\template\\";
    
    public static String getPickUpBookRequestXmlString(){
        try {
            Configuration cfg = new Configuration();   
            cfg.setDirectoryForTemplateLoading(new File(ftlUrl));   
            cfg.setObjectWrapper(new DefaultObjectWrapper());   
            /* 获取模板文件 */  
            Template template = cfg.getTemplate("pickup_book.ftl");   
            
            Map context = new HashMap();
            StringWriter writer = new StringWriter();
            
            context.put("MessageTime", "2001-12-17T09:30:47-05:00");
            context.put("MessageReference", "Esteemed Courier Service of DHL");
            context.put("SiteID", "DHL商务中心提供");
            context.put("Password", "DHL商务中心提供");
            
            context.put("AccountType1", "D");
            context.put("AccountNumber1", "550000055");
            context.put("PersonName", "Rikhil");
            context.put("Phone1", "23162");
            context.put("PhoneExtension", "5053");
            
            context.put("LocationType", "B");
            context.put("CompanyName", "String");
            context.put("Address1", "String");
            context.put("Address2", "norfolk st");
            context.put("PackageLocation", "Infosys");
            context.put("City", "Kuala Lumpur");
            context.put("StateCode", "MH");
            context.put("DivisionName", "California");
            context.put("CountryCode", "MY");
            context.put("PostalCode", "2516251");
            
            context.put("PickupDate", "2015-01-15");//填写时间
            context.put("ReadyByTime", "11:20");//一个小时之后(相对于现在时间)
            context.put("CloseTime", "13:55");//两个小时之后(相对于ReadyByTime)
            context.put("Pieces", "2");
            
            context.put("Weight1", "2.0");
            context.put("WeightUnit1", "K");
            
            context.put("PersonName", "Subhayu");
            context.put("Phone2", "4801313131");
            context.put("PhoneExtension", "5768");
            
            context.put("AccountType2", "D");
            context.put("AccountNumber2", "851624480");
            context.put("BillToAccountNumber", "100000000");
            context.put("AWBNumber", "7520067111");
            context.put("NumberOfPieces", "1");
            context.put("Weight2", "1.0");
            context.put("WeightUnit2", "K");
            context.put("GlobalProductCode", "D");
            context.put("DoorTo", "DD");
            context.put("DimensionUnit", "C");
            context.put("InsuredAmount", "999999.99");
            context.put("InsuredCurrencyCode", "USD");
            
                context.put("Weight3", "1.0");
                context.put("Width", "2");
                context.put("Height", "2");
                context.put("Depth", "2");
            
            context.put("SpecialService1", "S");
            context.put("SpecialService2", "I");
            try {
                template.process(context, writer);
                return writer.toString();
            } catch (TemplateException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }   
        return "";
    }
    
    public static void main(String[] args) throws IOException{
        String xmlString = DHLInterfaceUtils.getPickUpBookRequestXmlString();
        String httpUrl = "https://xmlpitest-ea.dhl.com/XMLShippingServlet";
        String resXmlString = getResXmlString(xmlString,httpUrl);
        System.out.println(resXmlString);
    } 
}
        


打印返回结果:


    
        
            2015-01-15T03:43:48+01:00
            Esteemed Courier Service of DHL
            prasanta
        
    
    
        Success
    
    101746
    2015-01-15
    0.000000
    KUL

 

 

 

 

你可能感兴趣的:(freemarker,java)