4

I am trying to download a file through BITS and the job is failing ( giving me a error) as i failed to mention "referer" in http header in the GET request.

        BitsManager manager = new BitsManager();           
        manager.EnumJobs(JobOwner.CurrentUser);
        BitsJob newJob = manager.CreateJob(j.filename, JobType.Download);
        j.jobID = newJob.JobId;

        newJob.AddFile(j.serverLink, "C:\\Downloads\\" + j.filename);
        newJob.Priority = JobPriority.ForeGround;
        newJob.MinimumRetryDelay = 60;
        manager.OnJobTransferred += new EventHandler<NotificationEventArgs>(manager_OnJobTransferred);
        manager.OnJobModified += new EventHandler<NotificationEventArgs>(manager_OnJobModified);
        newJob.Resume();

Is there a way to configure the header for the GET request for the jobs ?

Thanks a ton,

Sunny

justapples
  • 63
  • 5

2 Answers2

2

BITS lets you set custom headers on the request.

The BITS team at Microsoft now has a page on Calling into BITS from .NET and C# using reference DLLs plus a complete sample call BITS Manager on GitHub.

I've just tried a custom modification to the sample. In setJobPropertyControl.xaml.cs, I cast the job to an IBackgroundCopyJobHttpOptions like this:

var jobHttpOptions = job as BITS4.IBackgroundCopyJobHttpOptions;

I had to also do

using BITS4 = BITSReference4_0;

Then I could

jobHttpOptions.SetCustomHeaders (text); 

where text is the header you need to set. You can set mulitple headers by just concatenating a big string ("referer: http://www.example.com\r\nx-other-header: another header\r\n"). Note that HTTP headers must be seperated with \r\n!

PESMITH_MSFT
  • 350
  • 1
  • 9
1

Sounds to me like your HTTP server may not be compatible with the HTTP requirements of BITS

HTTP Requirements for BITS Downloads

BITS supports HTTP and HTTPS downloads and uploads and requires that the server supports the HTTP/1.1 protocol. For downloads, the HTTP server's Head method must return the file size and its Get method must support the Content-Range and Content-Length headers. As a result, BITS only transfers static file content and generates an error if you try to transfer dynamic content, unless the ASP, ISAPI, or CGI script supports the Content-Range and Content-Length headers.

BITS can use an HTTP/1.0 server as long as it meets the Head and Get method requirements.

To support downloading ranges of a file, the server must support the following requirements:

  • Allow MIME headers to include the standard Content-Range and Content-Type headers, plus a maximum of 180 bytes of other headers.

  • Allow a maximum of two CR/LFs between the HTTPheaders and the first boundary string.

For more information check this link

Mike Taylor
  • 2,376
  • 2
  • 17
  • 33