Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Request Headers in BITS (Microsoft Background Intelligent Transfer Service) in C#

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

like image 255
justapples Avatar asked Oct 28 '25 15:10

justapples


1 Answers

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!

like image 81
PESMITH_MSFT Avatar answered Oct 31 '25 12:10

PESMITH_MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!