Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been provided a web interface(Form) URL with many fields in order to import a local file
to that website.

this is how the URL looks like:
http://www.website.com/na/imports/ImportFile.jsp


There are fields like type of import(SBE, MIME) which is a radio button, input type(Excel,CSV,XML),
output type(Text, XML, HTML), a FileUpload control, user and password controls to authenticate before uploading the file and between other controls. I just mentioned this to give you an idea of how the form looks like.

I am trying to upload an .xls File programmatically from a C# console application using the HttpWebRequestClass but the response I get with the GetResponse() method is the HTML structure of the same form and at same time I debugged my code and I see statusCode = OK property, which for me this tell me that everything went OK and no error in the uploading.

honestly I don't know what response should I get, however when I upload the file manually the process works like a charm and I get a successful response and says that the file has been imported.

Any Idea how to upload this file with the other fields values to the mentioned URL using HTTP POST method programmatically?
Posted
Updated 4-Aug-14 5:08am
v5

1 solution

You simply need to study what is sent in HTTP request when you do it in the browser with a "real" HTML page and do the same in your HTTP request sent through System.Net.HttpWebRequest.

First approach would be just looking at the content on this page. To start with, take the source code of this page using you browser (something like View / Page Source). If it has the form, each form element with the attribute "name" will participate in the request. The HTTP request include all the data on such controls in the form in key-value pairs. You can do one simple trick: make a copy of this page and modify the form's the attribute action: set it's value to your own ASP.NET page where you can examine the HTTP request and see what's in it.

However, the page in question (too bad you did not share any information on it; how you even expected any answer? :-)) may not use the form and use Ajax instead; then you would need to study this Javascript code and see what is sent.

A totally different approach would be this: get some HTTP spy application. Some come as the browser plug-in. I for example, use the plug-in to my Seamonkey browser called "HttpFox". Activate it and see what is the content of HTTP request send from the "real" HTML page. Do the same in your code.

—SA
 
Share this answer
 
Comments
Santiago Fabian 4-Aug-14 15:59pm    
it does not use javascript. the form tag definition is like this:
<form method="post" enctype="multipart/form-data" name="TheForm"> all controls here </form>
I have studied the name/value pairs collection and I accurately know what values go in the HTTP POST request, thanks for the trick you recommended me of how to do this :).
Just to clarify my purpose is that I have this third party web interface that reads an excel file, which is a defined template with employee records in order to be inserted in a database, when I do the process manually in the "real" HTML page the records are inserted successfully (and the response I get in the browser is ------- Info: # Of Employees In File: 2 Info: ACCOUNT.NEW: 1 Info: ACCOUNT_HR.CHANGE.EMPLOYEE_ID: 1 Info: Done ------ ) but when I do this process programmatically no records are inserted and I just get the same plane HTML of this third party web interface.
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string replyFromServer = responseReader.ReadToEnd();
My "replyFromServer" variable contains the HTML text
any other approach of how to test or debug what is going on with post request?
Sergey Alexandrovich Kryukov 4-Aug-14 17:35pm    
I understand. I gave you the general recipe which covers all cases. Why would you need some other approach? If you still cannot match HTTP request to meet your requirements, please use the approach shown in my last paragraph: under some "HTTP Spy" application, do HTTP requests from "real" HTML page and your application and compare the result, to find out what you are missing. Probably you are already pretty close, it sounds like.
—SA
Santiago Fabian 5-Aug-14 10:15am    
As you said I was pretty close, In some way y wasn't correctly reading the bytes of my local file into the byte array. Thanks for your help, It help me a lot to inspect what was the post data of the http post request. Now all the request works great!
Sergey Alexandrovich Kryukov 5-Aug-14 13:55pm    
I told you you would resolve the problem soon! That's great; it's a pleasure to help someone who actually understands the advice and can make things working.

Good luck, call again.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900