c winform 上传文件到mysql_winform通过HttpWebRequest(post方式)上传文件和传递参数 | 学步园...

1 privatevoidbutton1_Click(objectsender, EventArgs e)2 {3 UploadFileHttpRequest(AppDomain.CurrentDomain.BaseDirectory.Trim()+"bb.txt");4 }5 privatestringUploadFileHttpRequest(stringfileName)6 {7 stringoutput=string.Empty;8 MemoryStream postStream=null;9 BinaryWriter postWriter=null;10 HttpWebResponse response=null;11 StreamReader responseStream=null;12 13 conststringCONTENT_BOUNDARY="----------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6";14 conststringCONTENT_BOUNDARY_PREFIX="--";15 16 try17 {18 UriBuilder uriBuilder=newUriBuilder("http://localhost:7408/WebT/t.aspx");19 HttpWebRequest request=(HttpWebRequest)WebRequest.Create(uriBuilder.Uri);20 request.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";21 request.Timeout=300000;22 request.ContentType="multipart/form-data; boundary="+CONTENT_BOUNDARY;23 postStream=newMemoryStream();24 postWriter=newBinaryWriter(postStream);25 //-- 参数26 //param['setType']27 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+28 "Content-Disposition: form-data; name=\"param['setType']\"\r\n\r\n"));29 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("2"));30 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));31 //param['startTime']32 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+33 "Content-Disposition: form-data; name=\"param['startTime']\"\r\n\r\n"));34 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(""));35 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));36 //param['endTime']37 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+38 "Content-Disposition: form-data; name=\"param['endTime']\"\r\n\r\n"));39 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(""));40 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));41 //param['resourceID']42 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+43 "Content-Disposition: form-data; name=\"param['resourceID']\"\r\n\r\n"));44 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("1398130"));45 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));46 //forwardUrl47 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+48 "Content-Disposition: form-data; name=\"forwardUrl\"\r\n\r\n"));49 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("/cs/showBatchToneInfoStart.action"));50 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));51 //uploadFiles52 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+53 "Content-Disposition: form-data; name=\"uploadFiles\"\r\n\r\n"));54 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(fileName));55 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));56 byte[] fileContent=File.ReadAllBytes(fileName);57 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"\r\n"+58 "Content-Disposition: form-data; name=\"FileContent\""+59 "filename=\""+ Path.GetFileName(fileName) +"\"\r\n\r\n"));60 postWriter.Write(fileContent);61 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes("\r\n"));62 postWriter.Write(Encoding.GetEncoding("gb2312").GetBytes(CONTENT_BOUNDARY_PREFIX+CONTENT_BOUNDARY+"--"));63 64 request.ContentLength=postStream.Length;65 request.Method="POST";66 Stream requestStream=request.GetRequestStream();67 postStream.WriteTo(requestStream);68 response=(HttpWebResponse)request.GetResponse();69 70 for(inti=0; i0) enc=Encoding.GetEncoding(response.ContentEncoding);79 }80 catch{ }81 responseStream=newStreamReader(response.GetResponseStream(), enc);82 output+="\r\n\r\n\r\n"+responseStream.ReadToEnd();83 }84 finally85 {86 if(postWriter!=null) postWriter.Close();87 if(postStream!=null)88 {89 postStream.Close();90 postStream.Dispose();91 }92 if(response!=null) response.Close();93 if(responseStream!=null)94 {95 responseStream.Close();96 responseStream.Dispose();97 }98 }99 returnoutput;100 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值