欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用HttpWebRequest模拟表单提交

程序员文章站 2023-02-20 21:47:35
1 using System; 2 using System.Collections.Specialized; 3 using System.IO; 4 using System.Net; 5 using System.Text; 6 7 namespace Allyn.Common 8 { 9 p... ......
  1 using system;
  2 using system.collections.specialized;
  3 using system.io;
  4 using system.net;
  5 using system.text;
  6 
  7 namespace allyn.common
  8 {
  9     public class httphelper
 10     {
 11         /// <summary>
 12         /// 获取指定路径数据
 13         /// </summary>
 14         /// <param name="requesturi">提交路径</param>
 15         /// <param name="cookie">cookie容器对象</param>
 16         /// <returns>字符串结果</returns>
 17         public static string getform(string requesturi, cookiecontainer cookie)
 18         {
 19             httpwebrequest request = webrequest.createhttp(requesturi);
 20             request.method = "get";
 21             request.cookiecontainer = cookie;
 22             request.contentlength = 0;
 23 
 24             webresponse response = request.getresponse();
 25             streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8);
 26             return reader.readtoend();
 27         }
 28 
 29         /// <summary>
 30         /// 默认表单提交
 31         /// </summary>
 32         /// <param name="requesturi">提交路径</param>
 33         /// <param name="postdata">提交数据</param>
 34         /// <param name="cookie">cookie容器对象</param>
 35         /// <returns>字符串结果</returns>
 36         public static string postform(string requesturi, namevaluecollection postdata, cookiecontainer cookie)
 37         {
 38             httpwebrequest request = webrequest.createhttp(requesturi);
 39             request.method = "post";
 40             request.contenttype = "application/x-www-form-urlencoded";
 41             request.cookiecontainer = cookie;
 42 
 43             stringbuilder stringbuilder = new stringbuilder();
 44             foreach (string key in postdata.keys)
 45             {
 46                 stringbuilder.appendformat("&{0}={1}", key, postdata.get(key));
 47             }
 48             byte[] buffer = encoding.utf8.getbytes(stringbuilder.tostring().trim('&'));
 49             stream requeststream = request.getrequeststream();
 50             requeststream.write(buffer, 0, buffer.length);
 51             requeststream.close();
 52 
 53             webresponse response = request.getresponse();
 54             streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8);
 55             return reader.readtoend();
 56         }
 57 
 58         /// <summary>
 59         /// 多部件表单提交
 60         /// </summary>
 61         /// <param name="requesturi">提交路径</param>
 62         /// <param name="postdata">提交数据.注:如果是文件路径,代表是文件.</param>
 63         /// <param name="cookie">cookie容器对象</param>
 64         /// <returns>字符串结果</returns>
 65         public static string postformmultipart(string requesturi, namevaluecollection postdata, cookiecontainer cookie)
 66         {
 67             string boundary = string.format("-----{0}", datetime.now.ticks.tostring("x"));
 68             httpwebrequest webrequest = webrequest.createhttp(requesturi);
 69             webrequest.cookiecontainer = cookie;
 70             webrequest.timeout = 120000;
 71             webrequest.method = "post";
 72             webrequest.contenttype = string.format("multipart/form-data; boundary={0}", boundary);
 73 
 74             stream requeststream = webrequest.getrequeststream();
 75             foreach (string key in postdata.keys)
 76             {
 77                 stringbuilder strbuilder = new stringbuilder();
 78                 strbuilder.appendformat("--{0}", boundary);
 79                 strbuilder.appendformat("\r\ncontent-disposition: form-data; name=\"{0}\"", key);
 80                 if (file.exists(postdata.get(key)))
 81                 {
 82                     strbuilder.appendformat(";filename=\"{0}\"\r\ncontent-type: multipart/form-data\r\n\r\n", path.getfilename(postdata.get(key)));
 83                     byte[] buffer = encoding.utf8.getbytes(strbuilder.tostring());
 84                     requeststream.write(buffer, 0, buffer.length);
 85                     //获取图片流
 86                     filestream filestream = new filestream(postdata.get(key), filemode.open, fileaccess.read);
 87                     binaryreader binaryreader = new binaryreader(filestream);
 88                     byte[] filebuffer = binaryreader.readbytes((int)filestream.length);
 89                     binaryreader.close();
 90                     filestream.close();
 91                     requeststream.write(filebuffer, 0, filebuffer.length);
 92                 }
 93                 else
 94                 {
 95                     strbuilder.appendformat("\r\n\r\n{0}\r\n", postdata.get(key));
 96                     byte[] buff = encoding.utf8.getbytes(strbuilder.tostring());
 97                     requeststream.write(buff, 0, buff.length);
 98                 }
 99             }
100 
101             byte[] boundarybuffer = encoding.utf8.getbytes(string.format("\r\n--{0}\r\n", boundary));
102             requeststream.write(boundarybuffer, 0, boundarybuffer.length);
103             requeststream.close();
104 
105             webresponse response = webrequest.getresponse();
106             streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8);
107             return reader.readtoend();
108         }
109     }
110 }