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,如需转载请自行联系原作者
相关文章
09JavaWeb基础 - Response类案例(下载文件)
09JavaWeb基础 - Response类案例(下载文件)
173 0
|
前端开发 安全
ajax请求的时候get 和post方式的区别
ajax请求的时候get 和post方式的区别
|
JSON 数据格式
okhttp3 模拟get、post(json参数传递,form表单提交)
本文是博主学习okhttp3 的记录,希望对大家有所帮助。
2070 0
|
11月前
POST请求传递参数(十一)
POST请求传递参数(十一)
|
PHP
PHP开发中$_GET请求转为$_POST获取参数的解决方案
PHP开发中$_GET请求转为$_POST获取参数的解决方案
67 0
|
移动开发 前端开发 小程序
基于wx.request封装类似axios的请求方法
基于wx.request封装类似axios的请求方法
314 0
|
JavaScript 前端开发 Java
表单提交和超链接请求传递参数的几种方式
表单提交和超链接请求传递参数的几种方式 转载 http://blog.csdn.net/Sky786905664/article/details/73770785 这段时间在使用easy-ui的datagrid,他有自己提交表单的方式,所以就整理整理页面对参数的提交方式: 注:下面代码都已经过测试。
1725 0