假如某网站有个表单,例如(url: http://localhost/login.aspx):
帐号
密码
我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 webclient.uploaddata 方法来实现,将所要上传的数据拼成字符即可,程序很简单:
string uristring = "http://localhost/login.aspx";
// 创建一个新的 webclient 实例.
webclient mywebclient = new webclient();
string postdata = "username=admin&password=admin";
// 注意这种拼字符串的contenttype
mywebclient.headers.add("content-type","application/x-www-form-urlencoded");
// 转化成二进制数组
byte[] bytearray = encoding.ascii.getbytes(postdata);
// 上传数据,并获取返回的二进制数据.
byte[] responsearray = mywebclient.uploaddata(uristring,"post",bytearray);
对于文件上传类的表单,例如(url: http://localhost/uploadfile.aspx):
文件
对于这种表单,我们可以使用
string uristring = "http://localhost/uploadfile.aspx";
// 创建一个新的 webclient 实例.
webclient mywebclient = new webclient();
string filename = @"c:/upload.txt";
// 直接上传,并获取返回的二进制数据.
byte[] responsearray = mywebclient.uploadfile(uristring,"post",filename);
还有一种表单,不仅有文字,还有文件,例如(url: http://localhost/uploaddata.aspx):
文件名
文件
对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数:
public byte[] uploaddata(
string address,
string method,
byte[] data
);
在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploaddata.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?(在我以前的一篇blog中,曾分析过这个问题:asp无组件上传进度条解决方案),最终的数据如下:
-----------------------------7d429871607fe
content-disposition: form-data; name="file1"; filename="g:/homepage.txt"
content-type: text/plain
宝玉:http://www.webuc.net
-----------------------------7d429871607fe
content-disposition: form-data; name="filename"
default filename
-----------------------------7d429871607fe--
所以只要拼一个这样的byte[] data数据post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其contenttype是不一样的,例如上面的这种,其contenttype为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了contenttype,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的contenttype传到webclient中(例如:webclient.headers.add("content-type", contenttype);)这样,就可以通过webclient.uploaddata 方法上载文件数据了。
具体代码如下:
生成二进制数据类的封装
using system;
using system.web;
using system.io;
using system.net;
using system.text;
using system.collections;
namespace uploaddata.common
...{
/**//// <summary>
/// 创建webclient.uploaddata方法所需二进制数组
/// </summary>
public class createbytes
...{
encoding encoding = encoding.utf8;
/**//// <summary>
/// 拼接所有的二进制数组为一个数组
/// </summary>
/// <param name="bytearrays">数组</param>
/// <returns></returns>
/// <remarks>加上结束边界</remarks>
public byte[] joinbytes(arraylist bytearrays)
...{
int length = 0;
int readlength = 0;
// 加上结束边界
string endboundary = boundary + "--/r/n"; //结束边界
byte[] endboundarybytes = encoding.getbytes(endboundary);
bytearrays.add(endboundarybytes);
foreach(byte[] b in bytearrays)
...{
length += b.length;
}
byte[] bytes = new byte[length];
// 遍历复制
//
foreach(byte[] b in bytearrays)
...{
b.copyto(bytes, readlength);
readlength += b.length;
}
return bytes;
}
public bool uploaddata(string uploadurl, byte[] bytes, out byte[] responsebytes)
...{
webclient webclient = new webclient();
webclient.headers.add("content-type", contenttype);
try
...{
responsebytes = webclient.uploaddata(uploadurl, bytes);
return true;
}
catch (webexception ex)
...{
stream resp = ex.response.getresponsestream();
responsebytes = new byte[ex.response.contentlength];
resp.read(responsebytes, 0, responsebytes.length);
}
return false;
}
/**//// <summary>
/// 获取普通表单区域二进制数组
/// </summary>
/// <param name="fieldname">表单名</param>
/// <param name="fieldvalue">表单值</param>
/// <returns></returns>
/// <remarks>
/// -----------------------------7d52ee27210a3c/r/ncontent-disposition: form-data; name=/"表单名/"/r/n/r/n表单值/r/n
/// </remarks>
public byte[] createfielddata(string fieldname, string fieldvalue)
...{
string texttemplate = boundary + "/r/ncontent-disposition: form-data; name=/"{0}/"/r/n/r/n{1}/r/n";
string text = string.format(texttemplate, fieldname, fieldvalue);
byte[] bytes = encoding.getbytes(text);
return bytes;
}
/**//// <summary>
/// 获取文件上传表单区域二进制数组
/// </summary>
/// <param name="fieldname">表单名</param>
/// <param name="filename">文件名</param>
/// <param name="contenttype">文件类型</param>
/// <param name="contentlength">文件长度</param>
/// <param name="stream">文件流</param>
/// <returns>二进制数组</returns>
public byte[] createfielddata(string fieldname, string filename,string contenttype, byte[] filebytes)
...{
string end = "/r/n";
string texttemplate = boundary + "/r/ncontent-disposition: form-data; name=/"{0}/"; filename=/"{1}/"/r/ncontent-type: {2}/r/n/r/n";
// 头数据
string data = string.format(texttemplate, fieldname, filename, contenttype);
byte[] bytes = encoding.getbytes(data);
// 尾数据
byte[] endbytes = encoding.getbytes(end);
// 合成后的数组
byte[] fielddata = new byte[bytes.length + filebytes.length + endbytes.length];
bytes.copyto(fielddata, 0); // 头数据
filebytes.copyto(fielddata, bytes.length); // 文件的二进制数据
endbytes.copyto(fielddata, bytes.length + filebytes.length); // /r/n
return fielddata;
}
属性#region 属性
public string boundary
...{
get
...{
string[] barray, ctarray;
string contenttype = contenttype;
ctarray = contenttype.split(';');
if (ctarray[0].trim().tolower() == "multipart/form-data")
...{
barray = ctarray[1].split('=');
return "--" + barray[1];
}
return null;
}
}
public string contenttype
...{
get ...{
if (httpcontext.current == null)
...{
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
}
return httpcontext.current.request.contenttype;
}
}
#endregion
}
}
在winform中调用
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using uploaddata.common;
using system.io;
namespace uploaddatawin
...{
/**//// <summary>
/// frmupload 的摘要说明。
/// </summary>
public class frmupload : system.windows.forms.form
...{
private system.windows.forms.label lblamigotoken;
private system.windows.forms.textbox txtamigotoken;
private system.windows.forms.label lblfilename;
private system.windows.forms.textbox txtfilename;
private system.windows.forms.button btnbrowse;
private system.windows.forms.textbox txtfiledata;
private system.windows.forms.label lblfiledata;
private system.windows.forms.button btnupload;
private system.windows.forms.openfiledialog openfiledialog1;
private system.windows.forms.textbox txtresponse;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public frmupload()
...{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.dispose();
}
}
base.dispose( disposing );
}
windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
...{
this.lblamigotoken = new system.windows.forms.label();
this.txtamigotoken = new system.windows.forms.textbox();
this.lblfilename = new system.windows.forms.label();
this.txtfilename = new system.windows.forms.textbox();
this.btnbrowse = new system.windows.forms.button();
this.txtfiledata = new system.windows.forms.textbox();
this.lblfiledata = new system.windows.forms.label();
this.btnupload = new system.windows.forms.button();
this.openfiledialog1 = new system.windows.forms.openfiledialog();
this.txtresponse = new system.windows.forms.textbox();
this.suspendlayout();
//
// lblamigotoken
//
this.lblamigotoken.location = new system.drawing.point(40, 48);
this.lblamigotoken.name = "lblamigotoken";
this.lblamigotoken.size = new system.drawing.size(72, 23);
this.lblamigotoken.tabindex = 0;
this.lblamigotoken.text = "amigotoken";
//
// txtamigotoken
//
this.txtamigotoken.location = new system.drawing.point(120, 48);
this.txtamigotoken.name = "txtamigotoken";
this.txtamigotoken.size = new system.drawing.size(248, 21);
this.txtamigotoken.tabindex = 1;
this.txtamigotoken.text = "";
//
// lblfilename
//
this.lblfilename.location = new system.drawing.point(40, 96);
this.lblfilename.name = "lblfilename";
this.lblfilename.size = new system.drawing.size(80, 23);
this.lblfilename.tabindex = 2;
this.lblfilename.text = "filename";
//
// txtfilename
//
this.txtfilename.location = new system.drawing.point(120, 96);
this.txtfilename.name = "txtfilename";
this.txtfilename.size = new system.drawing.size(248, 21);
this.txtfilename.tabindex = 3;
this.txtfilename.text = "";
//
// btnbrowse
//
this.btnbrowse.location = new system.drawing.point(296, 144);
this.btnbrowse.name = "btnbrowse";
this.btnbrowse.tabindex = 4;
this.btnbrowse.text = "浏览...";
this.btnbrowse.click += new system.eventhandler(this.btnbrowse_click);
//
// txtfiledata
//
this.txtfiledata.location = new system.drawing.point(120, 144);
this.txtfiledata.name = "txtfiledata";
this.txtfiledata.size = new system.drawing.size(168, 21);
this.txtfiledata.tabindex = 5;
this.txtfiledata.text = "";
//
// lblfiledata
//
this.lblfiledata.location = new system.drawing.point(40, 144);
this.lblfiledata.name = "lblfiledata";
this.lblfiledata.size = new system.drawing.size(72, 23);
this.lblfiledata.tabindex = 6;
this.lblfiledata.text = "filedata";
//
// btnupload
//
this.btnupload.location = new system.drawing.point(48, 184);
this.btnupload.name = "btnupload";
this.btnupload.tabindex = 7;
this.btnupload.text = "upload";
this.btnupload.click += new system.eventhandler(this.btnupload_click);
//
// txtresponse
//
this.txtresponse.location = new system.drawing.point(136, 184);
this.txtresponse.multiline = true;
this.txtresponse.name = "txtresponse";
this.txtresponse.size = new system.drawing.size(248, 72);
this.txtresponse.tabindex = 8;
this.txtresponse.text = "";
//
// frmupload
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(400, 269);
this.controls.add(this.txtresponse);
this.controls.add(this.btnupload);
this.controls.add(this.lblfiledata);
this.controls.add(this.txtfiledata);
this.controls.add(this.btnbrowse);
this.controls.add(this.txtfilename);
this.controls.add(this.lblfilename);
this.controls.add(this.txtamigotoken);
this.controls.add(this.lblamigotoken);
this.name = "frmupload";
this.text = "frmupload";
this.resumelayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
...{
application.run(new frmupload());
}
private void btnupload_click(object sender, system.eventargs e)
...{
// 非空检验
if (txtamigotoken.text.trim() == "" || txtfilename.text == "" || txtfiledata.text.trim() == "")
...{
messagebox.show("please fill data");
return;
}
// 所要上传的文件路径
string path = txtfiledata.text.trim();
// 检查文件是否存在
if (!file.exists(path))
...{
messagebox.show("{0} does not exist!", path);
return;
}
// 读文件流
filestream fs = new filestream(path, filemode.open,
fileaccess.read, fileshare.read);
// 这部分需要完善
string contenttype = "application/octet-stream";
byte[] filebytes = new byte[fs.length];
fs.read(filebytes, 0, convert.toint32(fs.length));
// 生成需要上传的二进制数组
createbytes cb = new createbytes();
// 所有表单数据
arraylist bytesarray = new arraylist();
// 普通表单
bytesarray.add(cb.createfielddata("filename", txtfilename.text));
bytesarray.add(cb.createfielddata("amigotoken", txtamigotoken.text));
// 文件表单
bytesarray.add(cb.createfielddata("filedata", path
, contenttype, filebytes));
// 合成所有表单并生成二进制数组
byte[] bytes = cb.joinbytes(bytesarray);
// 返回的内容
byte[] responsebytes;
// 上传到指定url
bool uploaded = cb.uploaddata("http://localhost/uploaddata/uploadavatar.aspx", bytes, out responsebytes);
// 将返回的内容输出到文件
using (filestream file = new filestream(@"c:/response.text", filemode.create, fileaccess.write, fileshare.read))
...{
file.write(responsebytes, 0, responsebytes.length);
}
txtresponse.text = system.text.encoding.utf8.getstring(responsebytes);
}
private void btnbrowse_click(object sender, system.eventargs e)
...{
if(openfiledialog1.showdialog() == dialogresult.ok)
...{
txtfiledata.text = openfiledialog1.filename;
}
}
}
}
完整的代码见附件: uploaddata.rar(38k)(http://bbs.openlab.net.cn/postattachment.aspx?postid=400927),解压后给web目录建个虚拟目录"uploaddata",其中uploadavatar.aspx是实际的上传处理页,如果上传成功,则返回文件名和文件类型等信息。default.aspx是asp.net页面来调用 webclient.uploaddata方法提交数据,uploaddatawin项目则是winform程序调用。