使用HTTP Post方法以编程方式上传文件C# [英] Upload a file with HTTP Post method Programmatically C#

查看:321
本文介绍了使用HTTP Post方法以编程方式上传文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提供了一个包含许多字段的网络界面(表格)网址,以便将本地文件

导入该网站。



这就是URL的样子:

http://www.website.com/na/imports/ImportFile.jsp





有一些字段,如导入类型(SBE,MIME),它是一个单选按钮,输入类型(Excel,CSV,XML),

输出类型(文本, XML,HTML),FileUpload控件,用户和密码控件,用于在上载文件之前以及其他控件之间进行身份验证。我刚刚提到这个,让你知道表单是怎么样的。



我试图用C#控制台应用程序以编程方式上传.xls文件b> HttpWebRequestClass 但是我用 GetResponse()方法获得的响应是​​同一表单的HTML结构,同时我调试了我的代码,我看到 statusCode = OK 属性,对我来说,这告诉我一切顺利,上传时没有错误。



老实说我不知道​​应该得到什么回应但是当我手动上传文件时,这个过程就像一个魅力,我得到一个成功的回复,并说文件已被导入。



任何想法如何使用HTTP POST方法以编程方式将此文件与其他字段值一起上传到提到的URL?

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?

推荐答案

您只需要研究发送的内容在浏览器中使用真实HTML p进行HTTP请求时年龄并在通过 System.Net.HttpWebRequest 发送的HTTP请求中执行相同操作。



第一种方法是只看这个页面上的内容。首先,使用浏览器(例如View / Page Source)获取此页面的源代码。如果它具有表单,则具有属性name的每个表单元素将参与该请求。 HTTP请求包括键值对形式的此类控件上的所有数据。你可以做一个简单的技巧:制作这个页面的副本并修改表单的属性 action :将它的值设置到你自己的ASP.NET页面,在那里你可以检查HTTP请求,看看里面有什么。



然而,有问题的页面(太糟糕了,你没有分享任何信息;你怎么想到任何答案?: - ))可能不使用表单而是使用Ajax;然后你需要学习这个Javascript代码,看看发送了什么。



一个完全不同的方法是:得到一些HTTP间谍应用程序。有些是作为浏览器插件出现的。例如,我使用名为HttpFox的Seamonkey浏览器插件。激活它,看看从真实HTML页面发送的HTTP请求的内容是什么。在你的代码中也这样做。



-SA
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


这篇关于使用HTTP Post方法以编程方式上传文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆