<form action="sql.aspx" method="post" enctype="multipart/form-data">
<input id="Text1" name="content" type="text" /><br />
<input id="Text2" name="uploadImg" type="text" /><br />
<input id="File1" type="file" name="image0" /><br />
<input id="Submit1" type="submit" value="submit" />
</form>
為了查看表單提交時,向服務(wù)端post了什么數(shù)據(jù),這里我使用Fiddler來查看。Fiddler確實是個不錯的工具,注意當(dāng)url主機地址是localhost時Fiddler捕獲不到,需要再localhost后加一點(.)即可,打開Fiddler,瀏覽帶上面form的page,輸入數(shù)據(jù)提交,此時在Fiddler中可看到post的數(shù)據(jù)了。下面是一部份數(shù)據(jù)的截圖。
分析其中的數(shù)據(jù)不難得出,一個表單中的數(shù)據(jù)域(input type="text")對應(yīng)的格式為-----------------------------7da119c1004a6
Content-Disposition: form-data; name="content"this is a txt value
一個文件(input type="file")對應(yīng)的格式為(通常為表單最后一個參數(shù))-----------------------------7da119c1004a6
Content-Disposition: form-data; name="image0"; filename="E:\CAI\875.jpg"
Content-Type: image/pjpeg[文件內(nèi)容]
結(jié)尾處是-----------------------------7da119c1004a6--有了上面的數(shù)據(jù)做參考,按照其格式組織數(shù)據(jù),post到服務(wù)端,同樣可以達(dá)到html form提交的效果。要特別注意其格式:如回車換行,差一個都可能得不到正確的響應(yīng),還有請求的Content-Length一定計算對。下面是一個參考:
代碼
代碼如下:
public string POSTfile(string v1,string v2, string file)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");//請求
WebRequest req = WebRequest.Create(@"http://localhost.:4944/WebSite1/getfile.aspx");
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;//組織表單數(shù)據(jù)
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"content\"");
sb.Append("\r\n\r\n");
sb.Append(v1);
sb.Append("\r\n");sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"uploadImg\"");
sb.Append("\r\n\r\n");
sb.Append("v2");
sb.Append("\r\n");sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"image0\"; filename=\"e:\\a.jpg\"");
sb.Append("\r\n");
sb.Append("Content-Type: image/pjpeg");
sb.Append("\r\n\r\n");string head = sb.ToString();
byte[] form_data = Encoding.UTF8.GetBytes(head);
//結(jié)尾
byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");//文件
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
//post總長度
long length = form_data.Length + fileStream.Length + foot_data.Length;
req.ContentLength = length;Stream requestStream = req.GetRequestStream();
//發(fā)送表單參數(shù)
requestStream.Write(form_data, 0, form_data.Length);
//文件內(nèi)容
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
//結(jié)尾
requestStream.Write(foot_data, 0, foot_data.Length);
requestStream.Close();//響應(yīng)
WebResponse pos = req.GetResponse();
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);
string html = sr.ReadToEnd().Trim();
sr.Close();
if (pos != null)
{
pos.Close();
pos = null;
}
if (req != null)
{
req = null;
}
return html;
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com