public int Upload(string filename)
        {
            Debug.Assert(IsLoggedIn, "IsLoggedIn");

            var request = WebRequest.CreateHttp(BaseUrl + "upload.json");
            var mp      = new MultipartRequest();

            request.CookieContainer = cookieContainer;
            request.Method          = "POST";
            request.ContentType     = mp.ContentType;
            request.Referer         = BaseUrl;
            request.Headers.Add("X-CSRFToken: " + csrfToken);

            using (var stream = Helpers.TryOpenRead(filename))
            {
                mp.AddFile(stream, filename, Path.GetFileName(filename));

                var requestStream = request.GetRequestStream();
                mp.WriteRequest(requestStream);
            }

            var streamReader = new StreamReader(request.GetResponseStream());
            var json         = streamReader.ReadToEnd();

            dynamic responseData = JObject.Parse(json);

            if (responseData.error != null)
            {
                throw new WebException("Unable to upload file: " + responseData.error);
            }

            int?uploadId = responseData.upload_id;

            return(uploadId.Value);
        }
Exemple #2
0
        public override LogFileHandlerResults Handle(string filename)
        {
            var request = WebRequest.CreateHttp(UploadUrl);
            var mp      = new MultipartRequest();

            request.Method      = "POST";
            request.ContentType = mp.ContentType;

            using (var stream = Helpers.TryOpenRead(filename))
            {
                mp.AddFile(stream, filename, FileField);

                var requestStream = request.GetRequestStream();
                mp.WriteRequest(requestStream);
            }

            var streamReader = new StreamReader(request.GetResponseStream());
            var html         = streamReader.ReadToEnd();
            var link         = LinkRegex.Search(html);

            return(new LogFileHandlerResults
            {
                Success = link != null,
                Output = link ?? html
            });
        }
        public void Login(string username, string password)
        {
            var request = WebRequest.CreateHttp(BaseUrl + "login.json");
            var mp      = new MultipartRequest();

            mp.AddField("username", username);
            mp.AddField("password", password);

            request.CookieContainer = cookieContainer;
            request.Method          = "POST";
            request.ContentType     = mp.ContentType;
            request.Referer         = BaseUrl;
            request.Headers.Add("X-CSRFToken: " + csrfToken);

            var requestStream = request.GetRequestStream();

            mp.WriteRequest(requestStream);

            var streamReader = new StreamReader(request.GetResponseStream());
            var json         = streamReader.ReadToEnd();

            dynamic responseData = JObject.Parse(json);

            if (responseData.error != null)
            {
                throw new WebException("Unable to log in: " + responseData.error);
            }

            csrfToken = responseData.csrftoken;
            Debug.Assert(csrfToken != null, "csrfToken != null");

            IsLoggedIn = true;
        }