Skip to main content
Published: March 17 2010, 11:37:00 AMUpdated: November 28 2020, 8:11:29 PM

Sample code for downloadFile using XML in C#

The code sample below demonstrates how to make an downloadFile call using XML.

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


using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.IO.Compression;
.
.
.

private void downloadFile(string jobID, string OutFileRefID)
{
    string TaskRefID = jobID;
    string FileRefID = OutFileRefID;
    string endPoint = "https://storage.ebay.com/FileTransferService";

//Build request string
    string downloadFileRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                                        + "<downloadFileRequest xmlns:sct=\"http://www.ebay.com/soaframework/common/types\" xmlns=\"http://www.ebay.com/marketplace/services\">"
                                        + "<taskReferenceId>" + TaskRefID + "</taskReferenceId>"
                                        + "<fileReferenceId>" + FileRefID + "</fileReferenceId>"
                                        + "</downloadFileRequest>";

    HttpWebRequest httpRequest;
    WebResponse webResponse;
    StringBuilder sbResp = new StringBuilder();

    System.Text.Encoding iso8859 = System.Text.Encoding.GetEncoding("ISO-8859-1");

    //Create the HTTP request and set the headers
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(endPoint);

    httpRequest.Headers.Add("X-EBAY-SOA-SECURITY-TOKEN", YourAuthToken);
    httpRequest.Headers.Add("X-EBAY-SOA-SERVICE-NAME", "FileTransferService");
    httpRequest.Headers.Add("X-EBAY-SOA-SERVICE-VERSION", "1.0.0");                             
    httpRequest.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "downloadFile"); 

    httpRequest.Method = "POST";

    //Put the data into a UTF8 encoded  byte array
    UTF8Encoding encoding = new UTF8Encoding();
    int dataLen = encoding.GetByteCount(downloadFileRequest);
    byte[] utf8Bytes = new byte[dataLen];
    Encoding.UTF8.GetBytes(downloadFileRequest, 0, downloadFileRequest.Length, utf8Bytes, 0);

    Stream Streamobj = null;
    
    //Set the request Stream
    Streamobj = httpRequest.GetRequestStream();

    //Write the request to the Request Steam
    Streamobj.Write(utf8Bytes, 0, utf8Bytes.Length);
    Streamobj.Close();

    //Get response into stream
    webResponse = httpRequest.GetResponse();
    Streamobj = webResponse.GetResponseStream();

    // Get Response into StringBuilder object
    char[] buf = new char[4096];
    int len;
    System.IO.StreamReader sr = new StreamReader(Streamobj, iso8859);

    try
    {
        while ((len = sr.Read(buf, 0, buf.Length)) > 0)
        {
            sbResp.Append(buf, 0, len);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

    sr.Close();
    Streamobj.Close();     

    if (sbResp.ToString() != "")
    {
        int StartIndex = sbResp.ToString().LastIndexOf("Content-ID: <urn:uuid:");
        int EndIndex = sbResp.ToString().LastIndexOf("--MIMEBoundaryurn_uuid_");
        EndIndex -= 2;

        //32 = Length of UUID, 4 for the 2 CRLFs
        StartIndex += "Content-ID: <urn:uuid:".Length + 32 + 4;

        string attchmnt = sbResp.ToString().Substring(StartIndex, (EndIndex - StartIndex));

        byte[] data = iso8859.GetBytes(attchmnt);

        string path = @"C:\downloadedFile.zip"; 

// Write to zip file
        FileStream fs = File.Create(path);
        fs.Write(data, 0, data.Length);
        fs.Close();

        Console.WriteLine(sbResp.ToString());
    }
    else
        Console.WriteLine("Error fetching response");
   
}  

 

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