We help IT Professionals succeed at work.

C# App post *.csv file to website

 I've created a small app that logs on to a website. I am not using a WebBrowser but using HttpWebRequest and WebResponse. So now I have:

StreamReader streamReader = new StreamReader(webresponse.GetResponseStream());
string html = streamReader.ReadToEnd();

In that html, somewhere, is a text box to add a file location and a POST method.

How do I target these properties/methods using HttpWebRequest?

Thank you.

Watch Question

Top Expert 2010


Once you find out the url to upload from the html, then for single file uploads using WebClient is simpler than using HttpWebRequest/POST

WebClient client = new WebClient();

byte[] responseBinary = client.UploadFile(url, fileName);

string response = Encoding.UTF8.GetString(responseBinary);

You can check out this link => http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx
Top Expert 2010

The html would typically have a form tag. The 'action' attribute has the url to upload.

<form action="http://www.site.com/upload" method="post" .....

The url in the action attribute will go to the url param when creating httprequest in HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

The filename and the actual file data will be written into the RequestStream returned as in Stream rs = wr.GetRequestStream();

A complete sample code is available here => http://cristiroma.wordpress.com/2010/06/08/how-to-programatically-upload-a-file-in-c-using-httpwebrequest/