Posting a file from C# to ASP.Net - c#

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.

Related

Handling Authentication for a File Display using a Web Service

This is my first time developing this kind of system, so many of these concepts are very new to me. Any and all help would be appreciated. I'll try to sum up what I'm doing as efficiently as possible.
Background: I have a web application running AngularJS with Bootstrap. The app communicates with the server and DB through a web service programmed using C#. On the site, users can upload files and reference them later using direct links. There's no restriction to file type (yet), so just about anything is allowed.
My Goal: Having direct links creates a big security problem for me, since the documents/images are supposed to be private data. What I would prefer to do is validate a user's credentials when the link is clicked, then load the file in the browser using a more generic url path.
--Example--
"mysite.com/attachments/1" ---> (Image)
--instead of--
"mysite.com/data/files/importantImg.jpg"
Where I'm At: Not very far. My first thought was to add a page that sends the server request and receives a file byte stream along with mime type that I can reassemble and present to the user. However, I have no idea if this is possible using a web service that sends JSON requests, nor do I have a clue about how the reassembling process would work client-side.
Like I said, I'll take any and all advice. I'd love to learn more about this subject for future projects as well, but for now I just need to be pointed in the right direction.
Your first thought is correct, for it, you need to use the Response object, and more specifically the AddHeader and Write functions. Of course this will be a different page that will only handle file downloads, so it will be perfectly fine in your JSON web service.
I don't think you want to do this with a web service. Just use a regular IHttpHandler to perform the validation and return the data. So you would have the URL "attachments/1" get rewritten to "attachments/download.ashx?id=1". When you've verified access, write the data to the response stream. You can use the Content Disposition header to set the file name.

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

how to create a win app which can interact with a website?

Hi
I'm trying to write a windows application which can read HTML content of a specific website and fill some data in some input fields and submit the page.
what I did untill now was reading page content from a WebBrowser object, navigated to the website.
I know that i need to create some request/response variables and work with them, but I have no good view on what i'm trying to do.
also, my information about HttpRequest and HttpResponse is so low...
HttpWebRequest is what you're looking for, examples for read/write to scrape abound.
WebClient might be simpler for you to implement but in the end is only a wrapper to the above.
You can try to use the approach described in the HttpWebRequest with https in C# thread as a starting point.

Getting data from a webpage

I have an idea for an App that would really help me out in work but I'm not sure if it's possible.
I want to run a C# desktop application that will ask for a value. When a value is supplied, the application will open a browswer, go to a webpage and add the value into a form on an online website. The form is then submitted and a new page is loaded that contains a table of results. I then want to extract the table of results from the page source and write code to parse the result values.
It is not important that the user see's this happen in an actual browser. In other words if there's a way to do it by reading HTTP requests then thats great.
The biggest problem I have is getting the values into the form and then retrieving the page source after the form is submitted and the next page loads.
Any help really appreciated.
Thanks
Provided that you're only using this in a legal context:
Usually, web forms are sent via POST request to the web server, specifically some script that handles it. You can look at the HTML code for the form's page and find out the destination for the form (form's action).
You can then use a HttpWebRequest in C# to "pretend you are the form", sending a POST request with all the required parameters (adding them to the HTTP header).
As a result you will get the source code of the destination page as it would be sent to the browser. You can parse this.
This is definitely possible and you don't need to use an actual web browser for this. You can simply use a System.Net.WebClient to send your HTTP request and get an HTTP response.
I suggest to use wireshark (or you can use Firefox + Firebug) it allows you to see HTTP requests and responses. By looking at the HTTP traffic you can see exactly how you should pass your HTTP request and which parameters you should be setting.
You don't need to involve the browser with this. WebClient should do all that you require. You'll need to see what's actually being posted when you submit the form with the browser, and then you should be able to make a POST request using the WebClient and retrieve the resulting page as a string.
The docs for the WebClient constructor have a nice example.
See e.g. this question for some pointers on at least the data retrieval side. You're going to know a lot more about the http protocol before you're done with this...
Why would you do this through web pages if you don't even want the user to do anything?
Web pages are purely for interaction with users, if you simply want data transfer, use WCF.
#Brian using Wireshark will result in a very angry network manager, make sure you are actually allowed to use it.

How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it?

How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it? Is there a way using HTTP methods (in C# in this case) to check prior to fully downloading it?
Really, you want to look for the Last-Modified header after issuing a HEAD request (rather than a GET). I wrote some code to get the HEAD via WebClient here.
You can check that whether the file is changed or not by requesting with HEAD.
Then, returned response header may include Last-Modified, or ETag if the web server support.
You can do a HEAD request and check the last-modified datetime value, as well as the content-length.

Categories

Resources