我們需要在程序中提交數(shù)據(jù)到這個(gè)表單,對(duì)于這種表單,我們可以使用 WebClient.UploadData 方法來(lái)實(shí)現(xiàn),將所要上傳的數(shù)據(jù)拼成字符即可,程序很簡(jiǎn)單 " /> 任你躁在线精品视频m3u8,国产99re,欧美在线精品永久免费播放

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

用WebClient.UploadData方法上載文件數(shù)據(jù)的方法

假如某網(wǎng)站有個(gè)表單,例如(url: http://localhost/login.ASPx): 
帳號(hào) 
密碼 

我們需要在程序中提交數(shù)據(jù)到這個(gè)表單,對(duì)于這種表單,我們可以使用 WebClient.UploadData 方法來(lái)實(shí)現(xiàn),將所要上傳的數(shù)據(jù)拼成字符即可,程序很簡(jiǎn)單: 

string uriString = "http://localhost/login.ASPx"; 
// 創(chuàng)建一個(gè)新的 WebClient 實(shí)例. 
WebClient myWebClient = new WebClient(); 
string postData = "Username=admin&Password=admin"; 
// 注意這種拼字符串的ContentType 
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); 
// 轉(zhuǎn)化成二進(jìn)制數(shù)組 
byte[] byteArray = Encoding.ASCII.GetBytes(postData); 
// 上傳數(shù)據(jù),并獲取返回的二進(jìn)制數(shù)據(jù). 
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray); 


對(duì)于文件上傳類(lèi)的表單,例如(url: http://localhost/uploadFile.ASPx): 
文件 

對(duì)于這種表單,我們可以使用 
String uriString = "http://localhost/uploadFile.ASPx"; 

// 創(chuàng)建一個(gè)新的 WebClient 實(shí)例. 
WebClient myWebClient = new WebClient(); 

string fileName = @"C:upload.txt"; 

// 直接上傳,并獲取返回的二進(jìn)制數(shù)據(jù). 
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName); 


還有一種表單,不僅有文字,還有文件,例如(url: http://localhost/uploadData.ASPx): 
文件名 
文件 

對(duì)于這種表單,似乎前面的兩種方法都不能適用,對(duì)于第一種方法,不能直接拼字符串,對(duì)于第二種,我們只能傳文件,重新回到第一個(gè)方法,注意參數(shù): 
public byte[] UploadData( 
string address, 
string method, 
byte[] data 
); 
在第一個(gè)例子中,是通過(guò)拼字符串來(lái)得到byte[] data參數(shù)值的,對(duì)于這種表單顯然不行,反過(guò)來(lái)想想,對(duì)于uploadData.ASPx這樣的程序來(lái)說(shuō),直接通過(guò)網(wǎng)頁(yè)提交數(shù)據(jù),后臺(tái)所獲取到的流是什么樣的呢?(在我以前的一篇blog中,曾分析過(guò)這個(gè)問(wèn)題:ASP無(wú)組件上傳進(jìn)度條解決方案),最終的數(shù)據(jù)如下: 

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


所以只要拼一個(gè)這樣的byte[] data數(shù)據(jù)Post過(guò)去,就可以達(dá)到同樣的效果了。但是一定要注意,對(duì)于這種帶有文件上傳的,其ContentType是不一樣的,例如上面的這種,其ContentType為"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我們就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我們就可以構(gòu)造出我們所需要的byte[] data了,最后,不要忘記,把我們構(gòu)造的ContentType傳到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)這樣,就可以通過(guò)WebClient.UploadData 方法上載文件數(shù)據(jù)了。 

具體代碼如下: 
生成二進(jìn)制數(shù)據(jù)類(lèi)的封裝 

using System; 
using System.Web; 
using System.IO; 
using System.NET
using System.Text; 
using System.Collections; 

namespace UploadData.Common 
...{ 
/**//// <summary> 
/// 創(chuàng)建WebClient.UploadData方法所需二進(jìn)制數(shù)組 
/// </summary> 
public class CreateBytes 
...{ 
Encoding encoding = Encoding.UTF8; 

/**//// <summary> 
/// 拼接所有的二進(jìn)制數(shù)組為一個(gè)數(shù)組 
/// </summary> 
/// <param name="byteArrays">數(shù)組</param> 
/// <returns></returns> 
/// <remarks>加上結(jié)束邊界</remarks> 
public byte[] JoinBytes(ArrayList byteArrays) 
...{ 
int length = 0; 
int readLength = 0; 

// 加上結(jié)束邊界 
string endBoundary = Boundary + "--rn"; //結(jié)束邊界 
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary); 
byteArrays.Add(endBoundaryBytes); 

foreach(byte[] b in byteArrays) 
...{ 
length += b.Length; 

byte[] bytes = new byte[length]; 

// 遍歷復(fù)制 
// 
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> 
/// 獲取普通表單區(qū)域二進(jìn)制數(shù)組 
/// </summary> 
/// <param name="fieldName">表單名</param> 
/// <param name="fieldValue">表單值</param> 
/// <returns></returns> 
/// <remarks> 
/// -----------------------------7d52ee27210a3crnContent-Disposition: form-data; name="表單名"rnrn表單值rn 
/// </remarks> 
public byte[] CreateFieldData(string fieldName, string fieldValue) 
...{ 
string textTemplate = Boundary + "rnContent-Disposition: form-data; name="{0}"rnrn{1}rn"; 
string text = String.Format(textTemplate, fieldName, fieldValue); 
byte[] bytes = encoding.GetBytes(text); 
return bytes; 



/**//// <summary> 
/// 獲取文件上傳表單區(qū)域二進(jìn)制數(shù)組 
/// </summary> 
/// <param name="fieldName">表單名</param> 
/// <param name="filename">文件名</param> 
/// <param name="contentType">文件類(lèi)型</param> 
/// <param name="contentLength">文件長(zhǎng)度</param> 
/// <param name="stream">文件流</param> 
/// <returns>二進(jìn)制數(shù)組</returns> 
public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes) 
...{ 
string end = "rn"; 
string textTemplate = Boundary + "rnContent-Disposition: form-data; name="{0}"; filename="{1}"rnContent-Type: {2}rnrn"; 

// 頭數(shù)據(jù) 
string data = String.Format(textTemplate, fieldName, filename, contentType); 
byte[] bytes = encoding.GetBytes(data); 



// 尾數(shù)據(jù) 
byte[] endBytes = encoding.GetBytes(end); 

// 合成后的數(shù)組 
byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length]; 

bytes.CopyTo(fieldData, 0); // 頭數(shù)據(jù) 
fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二進(jìn)制數(shù)據(jù) 
endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // rn 

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中調(diào)用 


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 的摘要說(shuō)明。 
/// </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> 
/// 必需的設(shè)計(jì)器變量。 
/// </summary> 
private System.ComponentModel.Container components = null; 

public frmUpload() 
...{ 
// 
// Windows 窗體設(shè)計(jì)器支持所必需的 
// 
InitializeComponent(); 

// 
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 
// 


/**//// <summary> 
/// 清理所有正在使用的資源。 
/// </summary> 
protected override void Dispose( bool disposing ) 
...{ 
if( disposing ) 
...{ 
if (components != null) 
...{ 
components.Dispose(); 


base.Dispose( disposing ); 


Windows 窗體設(shè)計(jì)器生成的代碼#region Windows 窗體設(shè)計(jì)器生成的代碼 
/**//// <summary> 
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改 
/// 此方法的內(nèi)容。 
/// </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> 
/// 應(yīng)用程序的主入口點(diǎn)。 
/// </summary> 
[STAThread] 
static void Main() 
...{ 
Application.Run(new frmUpload()); 


private void btnUpload_Click(object sender, System.EventArgs e) 
...{ 
// 非空檢驗(yàn) 
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)); 


// 生成需要上傳的二進(jìn)制數(shù)組 
CreateBytes cb = new CreateBytes(); 
// 所有表單數(shù)據(jù) 
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)); 

// 合成所有表單并生成二進(jìn)制數(shù)組 
byte[] bytes = cb.JoinBytes(bytesArray); 

// 返回的內(nèi)容 
byte[] responseBytes; 

// 上傳到指定Url 
bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.ASPx", bytes, out responseBytes); 

// 將返回的內(nèi)容輸出到文件 
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; 







完整的代碼見(jiàn)附件: UploadData.rar(38K)(http://bbs.openlab.NET.cn/PostAttachment.ASPx?PostID=400927),解壓后給web目錄建個(gè)虛擬目錄"UploadData",其中UploadAvatar.ASPx是實(shí)際的上傳處理頁(yè),如果上傳成功,則返回文件名和文件類(lèi)型等信息。default.ASPx是ASP.NET頁(yè)面來(lái)調(diào)用 WebClient.UploadData方法提交數(shù)據(jù),UploadDataWin項(xiàng)目則是winform程序調(diào)用。

AspNet技術(shù)用WebClient.UploadData方法上載文件數(shù)據(jù)的方法,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 中文字幕久久精品 | 天天综合干 | 午夜色大片在线观看 | 四虎com | 怡红院最新网址 | 永久免费在线观看视频 | 九九视频免费精品视频免费 | 在线亚洲一区二区 | 免费观看成人www精品视频在线 | 国产xx在线观看 | 国产99久9在线视频 国产99久久精品 | 在线观看视频www在线观看 | 成人亚洲网 | 伊人久久中文大香线蕉综合 | 欧美日韩一区二区三区自拍 | 色精品视频 | 久99久热只有精品国产男同 | 三级黄网站 | 福利视频精品 | 婷婷中文 | 级毛片久久久毛片精品毛片 | 这里是九九伊人 | 国产精品资源站 | 亚洲全网成人资源在线观看 | 五月天婷婷一区二区三区久久 | 国产高清一区二区三区视频 | 一区一区三区产品乱码 | 国产91精品高清一区二区三区 | 国产免费一区2区3区4区 | 日本xxx在线观看免费播放 | 高清在线观看免费 | 精品无码久久久久久国产 | 成人综合久久精品色婷婷 | 精品一区二区三区三区 | 亚洲香蕉伊综合在人在线 | 成人在线视频免费观看 | 国产精品视频在这里有精品 | 日本人妖系列 | 精彩视频一区二区 | 国产精品久久久久桃色tv | 久久93精品国产91久久综合 |