How to rename a file/folder on OneDrive using Patch-Request? - c#

I am about to write an App which synchronizes my local folder with the cloud. As far as I know the LiveSDK doesn't provide any method that would help me with that?
So after searching on the internet i found an example here : http://onedrive.github.io/items/move.htm
It is about moving a file, but there is also a name property which should changeable.
So how do I build the Request in C# ?
This is how I tried so far, do not really know how to build the URL, with what parameters and so on. Also, can i make a PATCH-Request with a WebClient?
string url = String.Format("https://apis.live.net/v5.0/" + fileid + "?access_token="+this.liveConnectClient.Session.AccessToken);
using (WebClient wc = new WebClient())
{
//wc.DownloadData(url);
wc.UploadData(url, "PATCH", null);
}
I would be grateful for any clues.

It doesn't look like WebClient can support PATCH requests. You'll likely need to use the HttpWebRequest method (https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx) which supports additional verbs. The console on http://dev.onedrive.com should give you a better understanding of how the requests are put together for different calls.

Related

Can FtpWebRequest get a file above the root folder? [duplicate]

Can someone tell me how to change directories using FtpWebRequest? This seems like it should be an easy thing to do, but I'm not seeing it.
EDIT
I just want to add...I don't have my heart set on FtpWebRequest. If there's a better (easier) way to do FTP in .NET please let me know.
Apparently there's no way to do it using a live connection, you need to change the uri to trick ftpwebrequest into using a different request (thanks Jon).
So I'm looking for a 3rd party client...
Some of the open source solutions I tried didn't work too well (kept crashing), but I found one open source solution that's passed some of my preliminary tests (.NET FTP Client).
There's a blog post from Mariya Atanasova which shows how you can fake it - basically you have to put the directory on the URL.
I suspect you may be better off with a dedicated FTP library though - one that doesn't try to force everything into the WebRequest way of doing things. I haven't personally used any 3rd party libraries for this, but a search for "FTP library .NET" finds lots of candidates.
Edit: jcolebrand (in case of 2006 blog linkrot possibility)
Many customers ask us how they can use the CWD command with our FtpWebRequest.
The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.
Let's say you're using the following format:
String uri = "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl";
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(uri);
Request.Method = "LIST";
The above example will bring you to your user's directory and list all the contents there. Now let's say you want to go 2 directories backwards and list the contents there (provided your user has permissions to do that). You close the previous FtpWebRequest and issue a new one with this uri
uri = "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/%2E%2E/%2E%2E";
This is equivalent to logging in with your user's credentials and then using cd ../../
Note: if you try using the ”..” directly without escaping them the uri class will strip them, so "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/../.." is equivalent to "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/"
Now let's say you want to go to another user's directory which is one level above the root. If you don't specify a user name and password it's equivalent to logging in as anonymous user. Then you issue a new FtpWebRequest with the following uri
"ftp://myFtpUrl/%2F/anotherUserDir"
This is equivalent to logging in as anonymous and then doing
Cd /
cd anotherUserDirectory
You have to close the current connection:
request.Close();
And open a new one with an other uri:
uri = "ftp://example.com/%2F/directory" //Go to a forward directory (cd directory)
uri = "ftp://example.com/%2E%2E" //Go to the previously directory (cd ../)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
Instead of using ListDirectory method of FTPWebRequest you can use ListDirectoryDetails method of FtpWebRequest .
From That you can use regular expression to get value you want.
Thats it,It work fine for me in my case

Downloading a file from an Amazon shared link

I have been tasked with finding a way to save files given a link to someones amazon bucket. These files are attachments uploaded to a ticketing system, and when i used their API to pull the ticket out, i am given a link like the one below(I removed all the private stuff and replaced with XXXXX)
https://s3.amazonaws.com/cdn.XXXXXXX.com/data/helpdesk/attachments/production/36012348362/original/1.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXFus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180808T205357Z&X-Amz-Expires=86400&X-Amz-Signature=XXXXXXXXXXXXXX-Amz-SignedHeaders=Host
As you can see, the links time out after 1 day. I am connecting 2 API's together, and i need to be able to upload an actual file into the second API instead of just placing a link that times out after a day. I have tried to use Webclient downloadfileAsync, but all i get is a blank file output. I can go to the link in my browser and do a right click, save image as, so i know its possible, but i havent had any luck programatically. Below is the code snipped i am currently using to try and download it.
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(url), "image.jpg");
}
Any insight or help would be awesome.
Thanks in advance!
I was able to get this working by not using the async version of DownloadFile. Thanks to all the people who helped me resolve this.

Checking for and Potential Malware When Downloading a URL

I am trying to enable users on a web application to download HTML strings of any URLs using ASP.NET MVC. I know that it would be very easy to download a page using the following code:
using (WebClient client = new WebClient ())
{
string code= client.DownloadString(URL);
//...
}
Now the question is whether I need to check the request for any possible or potential attacks or malwares ot etc?
P.S: How would I know what file type I am getting? If it is let's say an image file, the response (somewhere I don't know) should have the content-type. Where is it?
You can send a md5 hash of a file to Virus Total and see if a file is a known virus.

How to use WebClient.UploadFileAsync to upload files and POST Params as well?

I am using WebClient.UploadFileAsync to upload local files to a web server and I would also like to pass some parameters with the post as well. I would like to send a few fields that will tell the PHP code on the server specific locations on where to save the uploaded files.
I tried to put the fields directly into the url, for example:
WebClient client = new WebClient();
Uri uri = new Uri("http://example.com/upload.php?field1=test");
client.UploadFileAsync(uri, "POST", "c:\test.jpg");
The PHP code returns false for isset($_REQUEST['field1']).
Thank you for any suggestions.
NOTE: this question was also asked in very similar format for vb.net a while back, but it did not get any answers,
WebClient's UploadFile is designed to send only a file (as byte[]) as part of request. From my understanding, UploadFile method closes the request stream after writing the binaries.
In your scenario, you actually request is has two parts 1. file as byte[] 2. the file name as string.
To do this, you have to use HttpWebRequest or any other high level class capable of creating request.
Refer to the post http://www.codeproject.com/KB/cs/uploadfileex.aspx?display=Print
which does a similar job
This article goes into detail about what is needed to accomplish posting of fields while uploading files using WebClient.
Unfortunately, most file upload scenarios are HTML form based and may
contain form fields in addition to the file data. This is where
WebClient falls flat. After review of the source code for WebClient,
it is obvious that there is no possibility of reusing it to perform a
file upload including additional form fields.
So, the only option is to create a custom implementation that conforms
to rfc1867, rfc2388 and the W3C multipart/form-data specification that
will enable file upload with additional form fields and exposes
control of cookies and headers.
I would look into using the QueryString property of the WebClient to set the value of field1 (as well as any other QueryString parameters to the request).
NameValueCollection query = new NameValueCollection();
query.Add("field1", "test");
client.QueryString = query;
Reference: http://msdn.microsoft.com/en-us/library/system.net.webclient.querystring(v=VS.100).aspx

Posting a file from C# to ASP.Net

I have a C# client which once every hour needs to post some zip files to ASP.Net site. This needs to be completely automated with no user interaction.
Wondering the best way to go about it.
Ideally would like to post the file without setting up any non .aspx / .asp pages.
Thanks for the help!
It depends on what the target site expects as content type. If it is multipart/form-data then a simple WebClient should do the job:
using (var client = new WebClient())
{
byte[] result = client.UploadFile(
"http://foo.com/index.aspx", #"d:\foo\bar.zip"
);
// TODO: Handle the server response if necessary
}
Send a HttpRequest containing all the necessary information including the bytes of the file. Google should help you on this one.
Nevertheless, I don't understand why you don't want to use a non .aspx page for this. A generic handle (.ashx) is suitable for this. But I still suggest you use another way to upload that file, e.g. per FTP and use a service that watches the directoy with a FileWatcher to determine and act on changes
In order to automate the task, you can use a DispatcherTimer (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx), assigning a handler to the Tick event.

Categories

Resources