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,如需转载请自行联系原作者
相关文章
|
8月前
|
前端开发 安全
ajax请求的时候get 和post方式的区别
ajax请求的时候get 和post方式的区别
|
JSON 数据格式
okhttp3 模拟get、post(json参数传递,form表单提交)
本文是博主学习okhttp3 的记录,希望对大家有所帮助。
1586 0
|
5月前
|
Web App开发 前端开发 JavaScript
AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
33 0
|
5月前
POST请求传递参数(十一)
POST请求传递参数(十一)
|
6月前
|
JSON 前端开发 API
新建一个uniapp请求,并且封装request
新建一个uniapp请求,并且封装request
19 1
|
10月前
|
PHP
PHP开发中$_GET请求转为$_POST获取参数的解决方案
PHP开发中$_GET请求转为$_POST获取参数的解决方案
41 0
|
前端开发 JavaScript
tp5中前端js代码中ajax请求url问题
tp5中前端js代码中ajax请求url问题
141 0
|
JSON 数据格式
搭建一个包含多种Get请求和Post请求的工具类
在工作的过程中经常会遇到需要调用接口的场景,用得多了就写了一个请求的工具类,以后再遇到需要Get请求或者Post请求的情况直接调用就行。