PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [CODE][c#]simple API example



Pepeillo
14.04.2017, 12:30
Thanks for Filecrypt.cc. Sorry i don't speak german.
If it is not the right thread, please move it.

This is not a fully, exhaustive, implementation of Filecrypt.cc API. Far from it.
It's just a basic sample, but you can easily modify for your needs.
The http is an asynchronous call.
If you prefer JSON than XML, you can use JavaScriptSerializer().Deserialize.
You can use this code without any restriction.
Any comments are welcome.

usage:

string lFilecrypt = string.Empty;
using (filecrypt fc = new filecrypt())
lFilecrypt = fc.clFilecrypt(params);


using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace uploaderNet
{
internal class filecrypt : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~filecrypt()
{
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{

}

public filecrypt()
{

}

private void finishWReq(IAsyncResult wRes, HttpWebRequest wReq, out string lFilecrypt)
{
string s = string.Empty;
lFilecrypt = string.Empty;
using (HttpWebResponse wr = (HttpWebResponse)wReq.EndGetResponse(wRes))
if (wr.StatusCode == HttpStatusCode.OK)
using (Stream st = wr.GetResponseStream())
using (StreamReader sr = new StreamReader(st))
s = sr.ReadToEnd();

if((s.ToLower().Contains("error")) || string.IsNullOrEmpty(s))
throw new Exception("filecrypt.cc error");
MatchCollection mc = new Regex(@"<container><link>(.*?)</link>", RegexOptions.IgnoreCase).Matches(s);
if(mc.Count > 0)
lFilecrypt = mc[0].Groups[1].Value;
}

/// <summary>
/// Create Link Filefrypt.cc from links.
/// usage: string lFilecrypt = new filecrypt().clFilecrypt(params);
/// Or
/// string lFilecrypt = string.Empty;
/// using (filecrypt fc = new filecrypt())
/// lFilecrypt = fc.clFilecrypt(params);
/// more info:
/// http://filecrypt.cc/api.pdf
/// http://filecrypt.cc/docs/index.htm#api-containerV2-create
/// </summary>
/// <param name="idFilecrypt">your API key</param>
/// <param name="title">foldername</param>
/// <param name="links">links separated by \n</param>
/// <returns>complete url to folder</returns>
public string clFilecrypt(string idFilecrypt, string title, string links)
{
string s = "fn=container&sub=create&fmt=xml&api_key=" + idFilecrypt;
s += "&foldername=" + title;
//string s = "fn=containerV2&sub=create&fmt=xml&api_key=" + idFilecrypt;
//s += "&name=" + title;
//s += "&mirror_1=" + links;
//s += "&group=group1";
s += "&mirror[]=" + links;
s += "&captcha=0";
s += "&allow_cnl=1";
s += "&allow_dlc=1";
s += "&allow_links=0";

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(new Uri("https://www.filecrypt.cc/api.php"));

wReq.Timeout = 200000;
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.ContentType = "application/x-www-form-urlencoded";
wReq.Method = "POST";
//wReq.UserAgent = new util().UserAgent;
wReq.Accept = "*/*";
byte[] b = Encoding.UTF8.GetBytes(s);
wReq.ContentLength = b.Length;
string lFilecrypt = string.Empty;
try
{
using (Stream dataStream = wReq.GetRequestStream())
dataStream.Write(b, 0, b.Length);
wReq.BeginGetResponse(wRes => { finishWReq(wRes, wReq, out lFilecrypt); }, null);
}
catch (Exception ex)
{ }
if (string.IsNullOrEmpty(lFilecrypt))
throw new Exception("filecrypt.cc error");
return lFilecrypt;
}
}
}