Http上传文件

技术分享
 1  public class UpLoadFile
 2     {
 3         public static void UpLoadFiles(string fileName)
 4         {
 5             string fileType = Path.GetExtension(fileName);
 6             string upLoadParms = "?Service=notify" + "&fileType=" + fileType;
 7             WebRequest webRequest = HttpWebRequest.Create(new Uri("http://192.168.0.1:8080" + upLoadParms));
 8             System.Text.Encoding coding = System.Text.Encoding.UTF8;
 9             webRequest.ContentType = "application/x-www-form-urlencoded";
10             webRequest.Method = "POST";
11             System.IO.Stream requeStream = webRequest.GetRequestStream();
12             FileStream fileStream=new FileStream(fileName,FileMode.Open,FileAccess.Read);
13             BinaryReader binaryReader=new BinaryReader(fileStream,coding);
14             byte[] buffer = binaryReader.ReadBytes((int) fileStream.Length);
15             requeStream.Write(buffer,0,buffer.Length);
16             fileStream.Close();
17             requeStream.Close();
18             System.Net.WebResponse response = webRequest.GetResponse();
19             Stream responseStream = response.GetResponseStream();
20             StreamReader streamReader = new StreamReader(responseStream);
21             string returnVal = streamReader.ReadToEnd();
22             responseStream.Close();
23             response.Close();
24             
25         }
26     }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。