Sharepoint C# - How to Download a File from GetFileByServerRelativeUrl - c#

I've tried pretty much every code sample and technique recommended on the first few pages of Google. All of these samples fail though with a timeout or similar error.
static void Main(string[] args)
{
using (WebClient client = new WebClient { Credentials = new NetworkCredential("username", "password", "domain") })
{
var tmpFile = Path.GetTempFileName();
String url = "https://sharepoint.organisation.net/GetFileByServerRelativeUrl/folder/file.docx')/$value')/Files";
client.DownloadFile(new Uri(url), tmpFile);
//OR
// client.DownloadFileAsync(new Uri(url), tmpFile);
}
}
If anyone could give me a suggestion on why my code, regardless of method used to download, keeps timing out on the actual request, that would be great.
This method is to test file analysis on sharepoint.

Related

How Do You Download a Csv File With Restsharp Without Getting Logged Out?

Here is my code.
Now When I run the code, the output to my terminal from Console.WriteLine is correct and is what I want written in my csv file. However, when I check the example.csv file on my computer, it just has the html of the sign in page indicating that I was logged out. What is the problem here?
From my understanding using cookiejar allows me to store logins and should keep me logged in even with extra requests.
using System;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Extensions;
namespace Updater
{
class ScheduledBilling
{
public static void report()
{
var client = new RestClient("http://example.com");
client.CookieContainer = new System.Net.CookieContainer();
client.Authenticator = new SimpleAuthenticator("username", "xxx", "password", "xxx");
var request = new RestRequest("/login", Method.POST);
client.DownloadData(new RestRequest("/file", Method.GET)).SaveAs("example.csv");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
class MainClass
{
public static void Main(string[] args)
{
string test = "\r\n";
ScheduledBilling.report();
Console.Write(test);
}
}
}
Also another small related question. Why does it execute and produce in the terminal the new rest request in client.DownloadData when response refers to the original log-in request?
You didn't execute the login request before trying to download the CSV. Your code should look something like this:
public static void report()
{
//Creates the client
var client = new RestClient("http://example.com");
client.CookieContainer = new System.Net.CookieContainer();
client.Authenticator = new SimpleAuthenticator("username", "xxx", "password", "xxx");
//Creates the request
var request = new RestRequest("/login", Method.POST);
//Executes the login request
var response = client.Execute(request);
//This executes a seperate request, hence creating a new requestion
client.DownloadData(new RestRequest("/file", Method.GET)).SaveAs("example.csv");
Console.WriteLine(response.Content);
}

Downloading Large Google Drive files with WebClient in C#

I know there are tones of questions on this subject already. After reading all the threads, I decided to get a redirected URL in a confirmation HTML page and then use it as a direct link to download.
As you know, the original URL format of the direct download link is like this.
https://drive.google.com/uc?export=download&id=XXXXX..
But if the size of the target file is big, then it is like this.
https://drive.google.com/uc?export=download&confirm=RRRR&id=XXXXX..
I can get RRRR from the first downloaded data, so I need to try twice in order to download the real file. The concept is very simple enough but I can't get this to work.
class Test
{
class MyWebClient: WebClient
{
CookieContainer c = new CookieContainer();
protected override WebRequest GetWebRequest(Uri u)
{
var r = (HttpWebRequest) base.GetWebRequest(u);
r.CookieContainer = c;
return r;
}
}
static string GetRealURL(string filename)
{
// Some Jobs to Parse....
return directLink;
}
static void Main()
{
MyWebClient wc = new MyWebClient();
string targetLink = "https://drive.google.com/uc?export=download&id=XXXXXXX";
wc.DownloadFile(targetLink, "tempFile.tmp");
targetLink = GetRealURL("tempFile.tmp");
wc.DownloadFile(targetLink, "realFile.dat");
}
}
What did I wrong?
I can get the right download link from the first file, but I get another confirmation page file with another confirm code on the second try. I thought this was because of cookies, so I created my own WebClient class as you can see above.
Also I originally used DownloadFileAsync(), and changed it to DownloadFile() just in case, but the same result..
I'm still thinking it has something to do with cookie things.
What am I missing here?
I had this same problem but had solved it in an HttpClient. I tried via your approach with WebClient and was able to get it to work. You don't show your GetRealUrl() source, but i'm willing to bet in there lies the issue. Here's how I did it:
You need to parse the html response to get the url in the href attribute of the "download anyway" button. It will only have the relative url, (the /uc?export=download... part)
You need to replace the xml escape character & with &
Then you can build the url using thte domain https://drive.google.com
At which point you can download the file. Here's the source (used in a test WPF application):
class MyWebClient : WebClient
{
CookieContainer c = new CookieContainer();
protected override WebRequest GetWebRequest(Uri u)
{
var r = (HttpWebRequest)base.GetWebRequest(u);
r.CookieContainer = c;
return r;
}
}
private async void WebClientTestButtonGdrive_Click(object sender, RoutedEventArgs e)
{
using (MyWebClient client = new MyWebClient())
{
//get the warning page
string htmlPage = await client.DownloadStringTaskAsync("https://drive.google.com/uc?id=XXXXXXX&export=download");
//use HtmlAgilityPack to get the url with the confirm parameter in the url
HtmlDocument document = new HtmlDocument();
document.LoadHtml(htmlPage);
HtmlNode node = document.DocumentNode;
HtmlNode urlNode = node.SelectSingleNode(#"//a[contains(#href, 'XXXXXXX') and contains(#id, 'uc-download-link')]//#href");
string downloadUrl = urlNode.Attributes["href"].Value;
downloadUrl = downloadUrl.Replace("&", "&");
downloadUrl = "https://drive.google.com" + downloadUrl;
//download the file
if (File.Exists("FileToDownload.zip"))
File.Delete("FileToDownload.zip");
await client.DownloadFileTaskAsync(downloadUrl, "FileToDownload.zip");
}
}

retriving php variable in c#

I have a PHP script which redirects the user to a file download. Upon viewing this page in a web browser I am automatically prompted for a location to save the file, with the correct filename and extension inside the SaveFileDialog.
I wish to download this file using an application written in C#. How can I retrieve the filename and extension of the file that is included in the response from the PHP script?
I think have to read the PHP variable, but I have not found the correct method to read it.
The PHP variables in which I am storing the filename and extension are $file and $ext respectively.
I've read several questions here, but I'm confused. Some user speak about WebClient, others speak about HttpWebRequest.
Can you point me in the correct direction?
Take a look here, where the process of downloading and saving file is described.
Here's how to get file name from the request response headers:
String header = client.ResponseHeaders["content-disposition"];
String filename = new ContentDisposition(header).FileName;
And one more notice: here client is WebClient component. And here is how to use download with WebClient: enter link description here
------The full solution ----------------------------
As it turned out, your server uses authentication. That's why in order to download file we have to pass authentication. So, PLEASE write full details. And here's the code:
private class CWebClient : WebClient
{
public CWebClient()
: this(new CookieContainer())
{ }
public CWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
public CookieContainer CookieContainer { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = this.CookieContainer;
}
return request;
}
}
static void Main(string[] args)
{
var client = new CWebClient();
client.BaseAddress = #"http://forum.tractor-italia.net/";
var loginData = new NameValueCollection();
loginData.Add("username", "demodemo");
loginData.Add("password", "demodemo");
loginData.Add("login","Login");
loginData.Add("redirect", "download/myfile.php?id=1622");
client.UploadValues("ucp.php?mode=login", null, loginData);
string remoteUri = "http://forum.tractor-italia.net/download/myfile.php?id=1622";
client.OpenRead(remoteUri);
string fileName = String.Empty;
string contentDisposition = client.ResponseHeaders["content-disposition"];
if (!string.IsNullOrEmpty(contentDisposition))
{
string lookFor = #"=";
int index = contentDisposition.IndexOf(lookFor, 0);
if (index >= 0)
fileName = contentDisposition.Substring(index + lookFor.Length+7);
}//attachment; filename*=UTF-8''JohnDeere6800.zip
client.DownloadFile(remoteUri, fileName);
}
On my PC that works.

WebClient.UploadValues() not returning response

I'm trying to create a simple Imgur app that will upload images to their server via a WebClient.UploadValues() call. I found this code elsewhere in StackOverflow:
public static void PostToImgur(string ImageFilePath)
{
using (var w = new WebClient())
{
var values = new NameValueCollection
{
{ "key", API_KEY },
{ "image", Convert.ToBase64String(File.ReadAllBytes(ImageFilePath)) }
};
byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);
XDocument result = (XDocument.Load(new MemoryStream(response)));
}
}
I inserted a breakpoint on the line which returns the WebClient response, however it seems to skip it completely without throwing any exceptions or anything. This is not anything I've ever seen before in Visual Studio, so something leads me to believe that there may be something strange happening.
If it helps, I'm running this in a Windows 7 virtual machine in OSX. Anybody have any ideas why this may be happening?
You might want to try
using (WebClient client = new WebClient())
{
client.UseDefaultCredentials = true;
...
}
Found the issue, I was using an OAuth token to pass to Imgur, where this method is used to upload with the Anonymous API.
Getting a new API key for an anonymous application solved it.

Download PDFs through proxy

I have a list of URLs linking directly to PDFs on a database website. It would be very easy to automate the download process, except for the fact that I have to access the website through a proxy server. The code I've been trying to use has been this:
public void Download()
{
WebClient wb2 = new WebClient();
WebProxy proxy = new WebProxy("PROXY_URL:port", true);
proxy.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
GlobalProxySelection.Select = proxy;
try
{
for(int i = 0; i < URLList.Length; i++)
{
byte[] Data = DownloadData(URLList[i]);
FileStream fs = new FileStream(#"D:\Files\" + i.toString() + ".pdf", FileMode.Create)
fs.Write(Data, 0, Data.Length);
fs.Close();
}
}
catch(WebException WebEx)
{
MessageBox.Show(WebEx.Message);
}
}
public byte[] DownloadData(string path)
{
WebClient wb2 = new WebClient();
wb2.Credentials = new NetworkCredential("USERNAME","PASSWORD");
return wb2.DownloadData(path);
}
For some reason, it returns the error "(400): Bad Request" every time. I'm obviously able to get to these PDFs just fine through Firefox, so I'm wondering what I'm doing wrong here. I'm fairly new to programming in general, and very new to web protocols through C#. Any help would be appreciated.
use fiddler to work out the difference between the request your code is sending vs the one via your browser.
the 400 error is due to a malformed request; opposed to the proxy denying you (407) or the site requiring authentication (401).
Incidently, the line "wb2.Credentials = ..." is providing your username/password to the target server. is this intended?
Haven't used WebClient for a while, but you can use var request = HttpWebRequest.Create();
request.Proxy = proxy; request.GetResponse().GetResponseStream() and read the bytes using BinaryReader().
That will give you the byte array that you can write to a file using File.WriteAllBytes() rather than having to use a FileStream.
hth

Categories

Resources