[API] addfile C#
Posted: August 10th, 2010, 1:22 pm
Hey,
I am currently developing a client which allows me to manage my QNAP NAS downloads. The NAS has an Sabnzbd installation (0.5.0).
When I try to run the following code i get a 500 - internal server error:
I've searched the internet for different implementations. I added headers, i used the WebClient class ( UploadFile ).. but none really helped. I am not really in writing bloated code.. so what does the API really expect at "addfile" since there is no documentation about that?
I am currently developing a client which allows me to manage my QNAP NAS downloads. The NAS has an Sabnzbd installation (0.5.0).
When I try to run the following code i get a 500 - internal server error:
Code: Select all
using(Stream file = File.Open(item.Location, FileMode.Open))
{
return query.Request<bool>(string.Format(
"http://{0}:{1}/sabnzbd/api?mode=addfile&output=xml&name={2}&nzbfile={3}&apikey={4}",
this.Url,
this.Port,
Path.GetFileName(item.Location),
Path.GetFileName(item.Location),
this.ApiKey
), null, null, file);
}
Code: Select all
public T Request<T>(string url, IServiceQueryResult<T> resultParser, WebHeaderCollection headers, Stream file)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.PreAuthenticate = true;
request.Credentials = this._service.Credentials;
request.Headers = headers == null ? new WebHeaderCollection() : headers;
request.CookieContainer = this._service.Cookies;
request.Timeout = Qall.Data.Settings.getInstance().TimeOut;
if (file != null)
{
byte[] bufferFile = file.ToByteArray();
request.Method = "POST";
request.KeepAlive = false;
request.UserAgent = "Qall";
using (Stream s = request.GetRequestStream())
{
Task.Factory.FromAsync<byte[], int, int>(
s.BeginWrite,
a => s.EndWrite(a),
bufferFile,
0,
bufferFile.Length,
null,
TaskCreationOptions.None
);
}
}
Task<WebResponse> taskResponse = Task.Factory.FromAsync<WebResponse>(
request.BeginGetResponse,
a => request.EndGetResponse(a),
TaskCreationOptions.None
);
return (resultParser == null) ? default(T) : resultParser.Parse(taskResponse.Result);
}