C# Upload NZB to SABnzbd - EXAMPLE

Got a program that plays well with SABnzbd? Share it here!
Post Reply
T3chDad
Newbie
Newbie
Posts: 32
Joined: July 19th, 2011, 2:43 pm
Contact:

C# Upload NZB to SABnzbd - EXAMPLE

Post by T3chDad »

After seemingly weeks of wrestling with this, I have finally figured out reliable code for uploading NZBs to SABnzbd with C#. Thank you to pieteckhart and switch who's conversation in this post put me on the track to solving the problem.

Hopefully this helps someone avoid the trouble that I had getting this working. You may use this code however you like...just give credit where credit is due. ;D

Code: Select all

using System.IO;
using System.Net;

//===========================================================
// Upload NZB provided by command line to SABnzbd queue
// 2011 - Bob Templeton - T3chDad™
//===========================================================
private void UploadNZB(string FileSpec, string Host, string Port, string apikey, string category)
{
	// Create the boundary using unix EpochTime
	DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
	DateTime nowInUTC = DateTime.UtcNow;
	string EpochTime = ((nowInUTC - baseTime).Ticks / 10000).ToString();
	string boundary = "---------------------------" + EpochTime;
	// Create the body for the request (POST)
	string body = "--" + boundary + "\nContent-Disposition: form-data; name=\"name\"; filename=\"" + Path.GetFileName(FileSpec) + "\"\n";
	// StreamReader to read the NZB File
	TextReader tr = new StreamReader(FileSpec);
	// Read the NZB and add to the request body
	body += "Content-Type: application/x-nzb\n\n" + tr.ReadToEnd();
	// Category
	body += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"cat\"\n\n" + category;
	// Priority
	body += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"pp\"\n\n" + "-1";
	// Scripts
	body += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"script\"\n\n" + "Default";
	body += "\n--" + boundary + "--\n\n";
	// Build the URL
	string URL = "http://" + Host + ":" + Port + "/sabnzbd/api?mode=addfile&name=" + Path.GetFileName(FileSpec) + "&apikey=" + apikey;
	// Create a request using a URL that can receive a post. 
	WebRequest request = WebRequest.Create(URL);
	// Set the Method property of the request to POST.
	request.Method = "POST";
	// Create POST data and convert it to a byte array.
	byte[] byteArray = Encoding.UTF8.GetBytes(body);
	// Set the ContentType property of the WebRequest.
	request.ContentType = "multipart/form-data; boundary=" + boundary;
	// Set the ContentLength property of the WebRequest.
	request.ContentLength = byteArray.Length;
	// Get the request stream.
	Stream dataStream = request.GetRequestStream();
	// Write the data to the request stream.
	dataStream.Write(byteArray, 0, byteArray.Length);
	// Close the Stream object.
	dataStream.Close();
	// Get the response.
	WebResponse response = request.GetResponse();
	// Display the status.
	//MessageBox.Show(((HttpWebResponse)response).StatusDescription);
	// Get the stream containing content returned by the server.
	dataStream = response.GetResponseStream();
	// Open the stream using a StreamReader for easy access.
	StreamReader reader = new StreamReader(dataStream);
	// Read the content.
	string responseFromServer = reader.ReadToEnd();
	// Display the content.
	//MessageBox.Show(responseFromServer);
	// Clean up the streams.
	reader.Close();
	dataStream.Close();
	response.Close();
}

jippo
Newbie
Newbie
Posts: 9
Joined: September 1st, 2011, 5:52 am

Re: C# Upload NZB to SABnzbd - EXAMPLE

Post by jippo »

Here's the code in VB.NET (with an edit in the boundary, to get it working):

Code: Select all

Public Sub UploadNZB(ByVal FileSpec As String, ByVal host As String, ByVal port As String, ByVal apikey As String, ByVal category As String)
        Dim boundary As String = DateTime.Now.Ticks.ToString("x")

        Dim body As String = "--" + boundary + vbNewLine + "Content-Disposition: form-data; name=""name""; filename=""" + Path.GetFileName(FileSpec) + """" + vbNewLine

        Dim tr As TextReader = New StreamReader(FileSpec)

        body += "Content-Type: application/x-nzb" + vbNewLine + vbNewLine + tr.ReadToEnd()
        body += vbNewLine + "--" + boundary + vbNewLine + "Content-Disposition: form-data; name=""cat""" + vbNewLine + vbNewLine + category
        body += vbNewLine + "--" + boundary + vbNewLine + "Content-Disposition: form-data; name=""pp""" + vbNewLine + vbNewLine + "-1"
        body += vbNewLine + "--" + boundary + vbNewLine + "Content-Disposition: form-data; name=""script""" + vbNewLine + vbNewLine + "Default"
        body += vbNewLine + "--" + boundary + "--" + vbNewLine + vbNewLine

        Dim URL As String = "http://" + host + ":" + port + "/sabnzbd/api?mode=addfile&name=" + Path.GetFileName(FileSpec) + "&apikey=" + apikey

        Dim request As WebRequest = WebRequest.Create(URL)
        request.Method = "POST"

        Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(body)

        request.ContentType = "multipart/form-data; boundary=" + boundary
        request.ContentLength = byteArray.Length

        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

        Dim response As WebResponse = request.GetResponse()

        dataStream = response.GetResponseStream()

        Dim reader As StreamReader = New StreamReader(dataStream)

        Dim responseFromServer As String = reader.ReadToEnd()

        reader.Close()
        dataStream.Close()
        response.Close()
    End Sub
xtrasimplicity
Newbie
Newbie
Posts: 2
Joined: May 20th, 2012, 4:08 am

Re: C# Upload NZB to SABnzbd - EXAMPLE

Post by xtrasimplicity »

Thank you, T3chDad. :)

I wanted a quick, simple solution of automatically adding my downloaded NZB files to SABnzbd, once I open them - so I wrote an app, using your UploadNZB method, and everything works perfectly (for me). :)

If it interests you, my app can be found here:
http://forums.sabnzbd.org/viewtopic.php?f=6&t=10838

I have referenced you in both the app, and the readme file.

Regards,
XS
Post Reply