假如某網站有個表單,例如(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程序調用。