We have a setup of server and windows mobile device as a client. In server CSI script ready to accept single file from client.
In Desktop we have use WebClient.UploadFile method to upload file to server, but in windows mobile this isn't implemented, till now we haven't found any alternative method to achieve same.
Thanks in advance.
Ramanand
When using the .NET Compact Framework, you can use System.Net.HttpWebRequest instead of WebClient, which isn't supported on .NET CF.
Since WebClient is implemented on top of HttpWebRequest, you can do everything with HttpWebRequest that you can with WebClient, albeit with more code.
For example, to download the contents of a URL into a string, you can use this code:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
string html;
using (var r = request.GetResponse().GetResponseStream())
{
using(var r2 = (TextReader)new StreamReader(r))
{
html = r2.ReadToEnd();
}
}
You should be able to use the method in this post, you could maybe do some refactoring to fit your purpose better.
Upload files with HTTPWebrequest (multipart/form-data)
Related
I have some code that connects to an HTTP API, and is supposed to get an XML response. When the API link is placed in a browser, the browser downloads the XML as a file. However, when the code connects to the same API, HTML is returned. I've told the API owner but they don't think anything is wrong. Is there a way to capture the downloaded file instead of the HTML?
I've tried setting the headers to make my code look like a browser. Also tried using WebRequest instead of WebClient. But nothing works.
Here is the code, the URL works in the browser (file downloaded) but doesn't work for WebClient:
WebClient webClient = new WebClient();
string result = webClient.DownloadString(url);
The code should somehow get the XML file instead of the page HTML (actually the HTML doesn't appear in the browser, only the file).
The uri that you access may be a HTML page that have its own mechanism (like generating the actual download address which may be dynamically generated by the server and redirecting to it, in order to prevent external linking access) to access the real file.
It is supposed to use a browser background browser core like CefSharp to run the HTML and its javascript to let it navigate, and probably you may want to hook the download event to handle the downloading.
I think you need to add accept header to the WebClient object.
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.Accept] = "application/xml;q=1";
string result = webClient.DownloadString(url);
}
Thank you all for your input. In the end, it was caused by our vendor switching to TLS 1.2. I just had to force my code to use 1.2 and then it worked.
Email Link
So I currently work for an engineering company and we receive files via Aconex (Aconex is a web based document management system for all consulting teams on a given project). Currently we have a system where we download the files (there is a link in the email that leads to the Aconex website) from the email and file them in a dated folder under the specific project. I've attached an image of the Aconex email link.
Now for the issue. Sometimes it can be quite overwhelming when you receive 20+ project related emails in a day (on top of everything else) and some of these may slip through the gaps.
Basically I would like to automate this process somehow. I want the user to be able to add the email link to the application, hit 'Process' and the files are then downloaded and filed under the specific project.
I've got some basic programming experience (mainly in c#) and would like to use this as my first 'real world' programming project.
Any help that can be offered is really appreciated.
Thanks people!
So, actually you want to make HTTP request to download it from publicly available web server?
You would need these two in the header:
using System.IO;
using System.Net;
The IO will help you messing with the local file system paths, directories and etc. Check: https://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx
The Net namespace contains many options for creating requests or downloading files.
Then in a method you create a WebRequest instance, with e.g. Create static method that have an URI object as input:
var httpRequest = WebRequest.Create(urlObject);
// here you setup your stuff like authorization or request method etc:
httpRequest.Method = "GET";
httpRequest.Timeout = settings.timeout;
// and finally call the request (this is an async approach)
httpRequest.BeginGetResponse(getResponse, this);
Here you get the response
public void getResponse(IAsyncResult __result)
{
WebResponse response = httpRequest.EndGetResponse(__result);
// here you deal with the response
}
Or you may use simpler way:
var webClient = new WebClient();
Uri urlObject = new Uri("http://yourUrl");
String localPath = "local\\filesystem\\path.file";
webClient.DownloadFileAsync(urlObject, localPath, this);
Check: https://msdn.microsoft.com/en-us/library/w8bysebz(v=vs.110).aspx
Have I got your issue right?
Im looking for solution how can I upload some file using http request. I got the idea that I'll transfer my files by post and on PHP side I'll put files on the server.
How doin this?
var client = new System.Net.WebClient();
client.UploadFile(address, filename);
See UploadFile on MSDN.
Well I'd try the simplest possible thing that might work to start with - WebClient.UploadFile:
WebClient client = new WebClient();
client.UploadFile(url, file);
Of course, you'll have to write appropriate PHP code to handle the upload...
One more approach is to upload file via browser and handle upload request/response with Fiddler. After that, you can write exact request using HttpWebRequest via C#.
I am currently working on a fileUploader in the C# language for a image hosting site I develop for, I am currently wondering what the best approach is to uploading files to the server(all php/sql) with the client software being C#.
Also, how would I process these uploads in a PHP script, would you say something along the lines of:
upload.php?appfile=filepath?
Thanks,
You can use the WebClient Class and its UploadFile Method:
using (var wc = new WebClient())
{
wc.UploadFile("http://www.example.com/upload.php", "somefile.dat");
}
The UploadFile Method uses the POST method to upload the file to the HTTP resource; you need to set up your PHP script accordingly.
I'm using .Net.
Is it possible for a web form to upload an image from a web or FTP server? The files I need to submit are on the web. If this is possible, code snippet is appreciated.
Yes, it is possible.
You can use the WebClient class to interact with other web servers in .Net server-side code.
For example:
using(var client = new WebClient())
client.UploadFile("ftp://server/path", #"C:\path\to\file");
If the file is on a different website, you can write the following:
using(var client = new WebClient())
client.UploadData("ftp://server/path", client.DownloadData("http://server/path"));
You can read and write FTP, HTTP, and HTTPS urls interchangeably.