Skip to main content
Published: May 20 2009, 2:30:00 PMUpdated: November 28 2020, 5:29:08 PM

Use Java HttpsURLConnection class to make Feedback API xml request.

 

Summary

   This Java sample illustrates a use of javax.net.ssl.HttpsURLConnection class to send createDSRSummaryByPeriod Feedback API XML request to eBay Feedback API Web Service.


 Detailed Description

 

/*
© 2009-2013 eBay Inc., All Rights Reserved
Licensed under CDDL 1.0 - http://opensource.org/licenses/cddl1.php
*/

import java.io.*; 
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
*
* @author zhuyang
*/
public class FeedbackClient {

    private static String CALLNAME = "createDSRSummaryByPeriod";
    private static final String BASEURL = "https://svcs.ebay.com/FeedbackService";
    private static final String FEEDBACKSERVICE = "FeedbackService";
    private static String USERTOKEN = "eBay User Token";
    private static String VERSION = "1.0.0";

    public static void main(String[] args) throws Exception {
        javax.net.ssl.HttpsURLConnection conn = null;
        DataOutputStream out = null;
        try {
            URL url = new URL(BASEURL);
            //open up the connection
            conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setUseCaches(false); set HTTP headers 
            // http://developer.ebay.com/DevZone/feedback/Concepts/MakingACall.html#callstruct
            // set HTTP headers 

            conn.setRequestProperty("X-EBAY-SOA-OPERATION-NAME", CALLNAME);
            conn.setRequestProperty("X-EBAY-SOA-SERVICE-NAME", FEEDBACKSERVICE);
            conn.setRequestProperty("X-EBAY-SOA-SECURITY-TOKEN", USERTOKEN);
            conn.setRequestProperty("X-EBAY-SOA-SERVICE-VERSION", VERSION);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            out = new DataOutputStream(conn.getOutputStream());
            // construct the createDSRSummaryByPeriod request XML string
            String requestPayload = buildCreateDSRSummaryByPeriodRequestXMLString();
            System.out.println(requestPayload);
            //send the xml request payload to the server
            out.writeBytes(requestPayload);
            conn.connect();
            if (conn.getInputStream() != null) {
                // read an input stream from HttpsURLConnection to StringBuffer  object
                StringBuffer result = readURLInputStream(conn);
                // create Document object for the returned xml response string
                // and then retrieve the node value for the tag named 'jobId'
                Document doc = createDom(result.toString());
                if (doc != null) {
                    Node first = doc.getFirstChild();
                    getValueByTagName(first, "jobId");
                }
            }
        } catch (Exception e) {
            //handle the exception  
        } finally {
            if (out != null) {
                out.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    /*
     * Gets an input stream for the given HttpsURLConnection object
     * @param conn HttpsURLConnection
     * @return result StringBuffer
     */
    private static StringBuffer readURLInputStream(javax.net.ssl.HttpsURLConnection conn) {
        StringBuffer result = null;
        try {
            System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
            System.out.println(conn.getURL());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            result = new StringBuffer();
            while ((line = br.readLine()) != null) {
                result.append(line);
            }

            System.out.println(result.toString());
            br.close();
        } catch (Exception e) {
        }
        return result;
    }

    /*
     * Gets node value for the given Node object and nodeName string
     * @param n Node
     * @param nodeName String
     */
    private static String getValueByTagName(Node n, String nodeName) {
        String nodeValue = "";
        Node childNode = null;
        NodeList list = n.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node tmp = list.item(i);
            if (tmp.getNodeType() == Node.ELEMENT_NODE) {
                Element section = (Element) tmp;
                section.getFirstChild();
                childNode = (Node) section.getFirstChild();
                if (section.getNodeName().equalsIgnoreCase(nodeName)) {
                    System.out.print(section.getNodeName() + " : ");
                    if (childNode != null) {
                        nodeValue = childNode.getNodeValue();
                        System.out.println(nodeValue);
                    }
                }
            }
        }
        return nodeValue;
    }

    /*
     * Create Document object for the given xml String
     * @param xml String
     * @return doc Document
     */
    private static Document createDom(String xml)
            throws SAXException, IOException,
            ParserConfigurationException {
        StringReader sr = new StringReader(xml);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new InputSource(sr));
        return doc;
    }

    /*
     * Constructs the createDSRSummaryByPeriod request XML string
     * @return requestXmlString String
     */
    private static String buildCreateDSRSummaryByPeriodRequestXMLString() {
        String requestXmlString = "";
        Calendar now = Calendar.getInstance();
        Calendar fromTime = (Calendar) now.clone();

        fromTime.add(Calendar.DAY_OF_YEAR, -30);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");

        String requestStart = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><createDSRSummaryByPeriodRequest xmlns=\"http://www.ebay.com/marketplace/services\">";
        String dateRange = "<dateRange><dateFrom>"
                + sdf.format(fromTime.getTime())
                + "</dateFrom><dateTo>"
                + sdf.format(now.getTime())
                + "</dateTo></dateRange>";
        String dateRangeEventType = "<dateRangeEventType>ListingDate</dateRangeEventType>";
        String requestEnd = "</createDSRSummaryByPeriodRequest>";
        
        requestXmlString = requestStart + dateRange + dateRangeEventType + requestEnd;
        return requestXmlString;
    }
}

How well did this answer your question?
Answers others found helpful