winform通过HttpWebRequest(post方式)上传文件和传递参数

简介:

复制代码
  1    private   void  button1_Click( object  sender, EventArgs e)
  2          {
  3              UploadFileHttpRequest(AppDomain.CurrentDomain.BaseDirectory.Trim()  +   " bb.txt " );
  4          }
  5           private   string  UploadFileHttpRequest( string  fileName)
  6          {
  7               string  output  =   string .Empty;
  8              MemoryStream postStream  =   null ;
  9              BinaryWriter postWriter  =   null ;
 10              HttpWebResponse response  =   null ;
 11              StreamReader responseStream  =   null ;
 12 
 13               const   string  CONTENT_BOUNDARY  =   " ----------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6 " ;
 14               const   string  CONTENT_BOUNDARY_PREFIX  =   " -- " ;
 15 
 16               try
 17              {
 18                  UriBuilder uriBuilder  =   new  UriBuilder( " 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  =   new  MemoryStream();
 24                  postWriter  =   new  BinaryWriter(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                   // forwardUrl
 47                  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                   // uploadFiles
 52                  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  ( int  i  =   0 ; i  <  response.Headers.Count; i ++ )
 71                  {
 72                      output  +=  response.Headers.Keys[i]  +   " "   +  response.GetResponseHeader(response.Headers.Keys[i])  +   " \r\n " ;
 73                  }
 74                  Encoding enc  =  Encoding.GetEncoding( " gb2312 " );
 75 
 76                   try
 77                  {
 78                       if  (response.ContentEncoding.Length  >   0 ) enc  =  Encoding.GetEncoding(response.ContentEncoding);
 79                  }
 80                   catch  { }
 81                  responseStream  =   new  StreamReader(response.GetResponseStream(), enc);
 82                  output  +=   " \r\n\r\n\r\n "   +  responseStream.ReadToEnd();
 83              }
 84               finally
 85              {
 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               return  output;
100          }



本文转自94cool博客园博客,原文链接http://www.cnblogs.com/94cool/archive/2010/11/26/1888909.html,如需转载请自行联系原作者
相关文章
|
7月前
|
JSON 前端开发 数据安全/隐私保护
通过一个form表单的渲染演示,Flask的request,重定向语法
通过一个form表单的渲染演示,Flask的request,重定向语法
59 0
通过一个form表单的渲染演示,Flask的request,重定向语法
|
JSON 前端开发 API
新建一个uniapp请求,并且封装request
新建一个uniapp请求,并且封装request
45 1
|
PHP
PHP开发中$_GET请求转为$_POST获取参数的解决方案
PHP开发中$_GET请求转为$_POST获取参数的解决方案
73 0
|
JSON 前端开发 数据格式
SpringMVC学习笔记(获取参数,传递参数——关于前后端传参交互的总结、from表单、Ajax数据提交))(下)
SpringMVC学习笔记(获取参数,传递参数——关于前后端传参交互的总结、from表单、Ajax数据提交))(下)
126 0
|
前端开发 Java API
SpringMVC学习笔记(获取参数,传递参数——关于前后端传参交互的总结、from表单、Ajax数据提交))(上)
SpringMVC学习笔记(获取参数,传递参数——关于前后端传参交互的总结、from表单、Ajax数据提交))(上)
189 0
|
移动开发 前端开发 小程序
基于wx.request封装类似axios的请求方法
基于wx.request封装类似axios的请求方法
330 0
|
存储 Java
初识 JSP---(Cookie / 重写URL / 防止表单重复提交)
Cookie Cookie是servlet发送到web浏览器的少量信息,这些信息由浏览器保存,然后发送回服务器, cookie一般用来保存session的id的会话。
1649 0