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 }
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,如需转载请自行联系原作者