Saving an image from HTTP Post (Not using FileUpload Control) - c#

I have a page (May even use a handler), anyway.. that is awaiting an HTTP POST with a number of variables including an image encoded as a multipart/form-data Content Type.
How do I save the image from the HTTP Post?
This image encoded int the HTTP POST is from another site and is not from a local FileUpload Control.
EDIT :
Hang on, am I being daft.. can't I just use Request.Files..
May be as simple as http://dotnetslackers.com/articles/aspnet/Upload_multiple_files_using_the_HtmlInputFile_control.aspx
-- Lee

if the posted form is multipart/form-data you can just check the Request.Files collection to check if there should be any files posted with your form :)

Related

Html img tag is not showing image from url in asp.net

<img src="http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542">
The above tag shows image when i create notepad html page but when i put it on asp.net it does not show image on the browser.
It doesn't work because the server behind the URL in question checks the HTTP referer and returns an empty page if a referer is set which doesn't match the expected one.
The referer tells the "vclogos" server from which website the request for loading the image came from.
This is probably a security measure from the page because they don't want people to do what you just tried to do: hotlink their images.
The thing is, when you test it from a "notepad html page", no referer is sent. This is also for security reasons, but from your browser's side: Sending file:///C:/Users/JohnDoe/Desktop/MyPrivateFiles/page.html as referer URL would disclose private information from your computer. That's why nothing is sent. Therefore, the image is loaded - the same as if you copy and paste the URL into the address bar of your browser (then also no referer is sent).
However, when you include it on your website on the Internet, your page URL is sent as referer. The "vclogos" website now appears to check this value and return nothing if a referer is sent which is not one of their own (I'd guess they accept only http://www.vclogos.co.uk/something so it will only work if it's included in their own website, not yours).
Long story short, you just can't load this image inside your website directly from this URL.
The only way would be to use a web request on the server side in ASP.NET which fetches the actual image and then serves it to the client as a resource which, from the browser's point of view, comes from your server (who got it from "vclogos" first, but without sending a referer). The thing is that if your server fetches the image data, you have control over what headers you send. (Unlike your website which, once loaded in the browser, doesn't have this control - the browser will send your page URL as referer and there is nothing you can do about that.)
Just a detail:
http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542
is a link to a blank page...
http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542
is the link to an image.
You are sending an encoded URL. Always use the decoded URL (such as urlencode in php or HttpServerUtility.UrlEncode Method (String) in aspx)

C# upload file to website

I am attempting to post a reply to an image board website.
It can be done through a winform web browser control or http request.
The issue is that with a post, you can upload an image with a input type file element on the page
<input type="file" />
For security reasons, I cannot set the value of the element to the file I want to upload.
When I use tamper data to see what is passed to the posting page, this is parameter that is passed under POST_DATA
-----------------------------256672629917035
Content-Disposition: form-data; name="upfile"; filename="image_file_name.jpg"
Content-Type: image/jpeg
So how is it possible to simulate a file upload of a input element in C#?
Look closely at the data that was posted, no directory is specified.
You can simulate any HTTP request through the HttpWebRequest object. Click here for an example.
When you're using the input type file element the browser is creating the request this for you. If you simply recreate this in HttpWebRequest you have full control over all aspects of the request and can modifiy as you wish.
It's probably in your interest to grab a copy of Fiddler.
If I understood your problem right,
you can't determine the directory of the file which was uploaded from your web browser control.
So ... it is impossible. The information about directory isn't given by browser - so it shouldn't be given by web browser control.
As MizardX suggested, it's better to send request using C#.

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

Forcing image to download

I'm using a WebBrowser control in Silverlight and I'm setting it to a local HTML page. The HTML page has various links and they all work fine. Can I make it so that if the user clicks on an image file, it downloads to their system (or does the default behavior of the broweser) instead of displaying on the webpage? The main question is, is it possible to do this if I don't have access to the server itself? Thanks
edit - Is it possible to send an HTTPWebRequest to get the image and then edit the response headers, all from the client? This may be an alternative.
The standard way of doing this is to send the Content-Disposition HTTP header with attachment as the value. See here for more on this: Uses of content-disposition in an HTTP response header
But if you don't have access to the server, I don't think you can achieve this.

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.

Categories

Resources