📜 ⬆️ ⬇️

.Net Client for mail.ru cloud

image

By the title of the article, I think everyone has already guessed what it will be about. As is known, the official API for the mail.ru cloud does not yet exist, so we will try to write it ourselves. This will of course be about the client side, and not about the server.

What was used and what you need to know to write it:
')
- C # programming language
- Visual Studio Development Environment
- Basic knowledge of POST and GET requests
- Browser

Actually, on what all this will rest, our API will only repeat some browser requests to the cloud.mail.ru server. When performing any operation with files or folders on the cloud, the browser will send http requests to the server, to which it will respond and give the answer you need.

The simplest example of a request to delete a file or folder. I will use Chrome Browser to track requests, where:

- Select the file or folder you want to delete
- Press F12
- Go to the Network tab
- Click "Delete" on the file or folder that we selected
- In the Network tab we see requests, among which we see a request called remove

Remove Request
General

Request URL:https://cloud.mail.ru/api/v2/file/remove Request Method:POST Status Code:200 OK Remote Address:217.69.139.7:443 

Response Headers

 Cache-Control:no-store, no-cache, must-revalidate Connection:close Content-Length:96 Content-Security-Policy-Report-Only:default-src *.cloud.mail.ru *.cloud.mail.ru *.datacloudmail.ru *.cldmail.ru *.mail.ru *.imgsmail.ru *.files.attachmail.ru *.mradx.net *.gemius.pl *.weborama.fr *.adriver.ru *.serving-sys.com featherservices.aviary.com d42hh4005hpu.cloudfront.net dme0ih8comzn4.cloudfront.net feather-client-files-aviary-prod-us-east-1.s3.amazonaws.com ; script-src 'unsafe-inline' 'unsafe-eval' *.cloud.mail.ru *.datacloudmail.ru *.cldmail.ru *.mail.ru *.imgsmail.ru *.files.attachmail.ru *.mradx.net *.yandex.ru *.odnoklassniki.ru odnoklassniki.ru *.ok.ru ok.ru *.scorecardresearch.com www.google-analytics.com featherservices.aviary.com d42hh4005hpu.cloudfront.net dme0ih8comzn4.cloudfront.net feather-client-files-aviary-prod-us-east-1.s3.amazonaws.com; img-src data: *; style-src 'unsafe-inline' *.mail.ru *.imgsmail.ru *.files.attachmail.ru *.mradx.net featherservices.aviary.com d42hh4005hpu.cloudfront.net dme0ih8comzn4.cloudfront.net feather-client-files-aviary-prod-us-east-1.s3.amazonaws.com; font-src data: cloud.mail.ru *.imgsmail.ru *.files.attachmail.ru *.mradx.net featherservices.aviary.com d42hh4005hpu.cloudfront.net dme0ih8comzn4.cloudfront.net feather-client-files-aviary-prod-us-east-1.s3.amazonaws.com; frame-src *.mail.ru *.datacloudmail.ru *.cldmail.ru docs.mail.ru *.officeapps.live.com *.mradx.net; object-src data: blob: https://*; report-uri https://cspreport.mail.ru/cloud/; Content-Type:application/json; charset=utf-8 Date:Sun, 10 Apr 2016 13:43:38 GMT Expires:Sat, 11 Apr 2015 13:43:38 GMT Pragma:no-cache Server:Tengine Strict-Transport-Security:max-age=15768000; includeSubDomains; preload X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN X-Host:clof8.i.mail.ru X-req-id:OljB4ucgKW X-server:api X-UA-Compatible:IE=Edge X-Upstream-Time:1460295818.308 X-XSS-Protection:1; mode=block; report=https://cspreport.mail.ru/xxssprotection 

Request Headers

 Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4 Connection:keep-alive Content-Length:181 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 Cookie: ,    Host:cloud.mail.ru Origin:https://cloud.mail.ru Referer:https://cloud.mail.ru/home/ User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36 X-Requested-With:XMLHttpRequest 

Form data

 view URL encoded home:/rename folder test (1) api:2 build:hotfix-35-1.201604041616 x-page-id:DcpfqVgGGm email:erastmorgan@bk.ru x-email:erastmorgan@bk.ru token:9qFgR48wHY5Rxqot4Ei515TiQMYCsPxD 


We need to repeat the same request from our API. For this, we will use the HttpWebRequest and HttpWebResponse classes. Here, the request should look exactly the same as our browser above, with the exception of some headers that are not mandatory. See the example below for deleting a file or folder.

Remove Request C #
I pulled this code straight from my project. It's pretty simple and I don't think it needs comments.

 /// <summary> /// Remove file or folder. /// </summary> /// <param name="name">File or folder name.</param> /// <param name="fullPath">Full file or folder name.</param> private void Remove(string name, string fullPath) { var removeRequest = Encoding.UTF8.GetBytes(string.Format("home={0}&api={1}&token={2}&email={3}&x-email={3}", fullPath, 2, this.Account.AuthToken, this.Account.LoginName)); var url = new Uri(string.Format("{0}/api/v2/file/remove", ConstSettings.CloudDomain)); var request = (HttpWebRequest)WebRequest.Create(url.OriginalString); request.Proxy = this.Account.Proxy; request.CookieContainer = this.Account.Cookies; request.Method = "POST"; request.ContentLength = removeRequest.LongLength; request.Referer = string.Format("{0}/home{1}", ConstSettings.CloudDomain, fullPath.Substring(0, fullPath.LastIndexOf(name))); request.Headers.Add("Origin", ConstSettings.CloudDomain); request.Host = url.Host; request.ContentType = ConstSettings.DefaultRequestType; request.Accept = "*/*"; request.UserAgent = ConstSettings.UserAgent; using (var s = request.GetRequestStream()) { s.Write(removeRequest, 0, removeRequest.Length); using (var response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(); } } } } 


For any other operation in the mail.ru cloud, we repeat the above steps, substituting our data in the place of those you caught from the browser.

I provide a list of methods that I wrote in my API:

- Create folder
- Copy file
- Copy folder
- Download file (Async operation)
- Upload file (Async operation)
- Get list of files and folders
- Get public file link
- Get public folder link
- Get direct file link (operation on one session)
- Move file
- Move folder
- Rename file
- Rename folder
- Remove file
- Remove folder

The API that I wrote you can download by reference . Send a Pull Request if you have time and you want to add new features to this API. All this is written “on the knee”, but the main thing works and can be freely used in your projects.

In the future, this API will still be supplemented, for example, I would also like to add file encryption and large file uploads (now the limit is 2GB), with the condition that a large file will be split into the maximum possible parts when downloaded to the cloud, and when unloaded it was going back .

Source: https://habr.com/ru/post/281360/


All Articles