CSharp-form-data发送类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class HttpClientHelper
{
private static string zhongtaiUrl = ConfigurationManager.AppSettings["ZhongTaiUrl"];

/// <summary>
/// 用multipart/form-data发送
/// </summary>
/// <param name="postParaList"></param>
/// <returns></returns>
public static string PostMessage(List<PostDataClass> postParaList)
{
try
{
string responseContent = "";
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(zhongtaiUrl);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("X");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
memStream.Write(beginBoundary, 0, beginBoundary.Length);
// 设置属性
webRequest.Method = "POST";
webRequest.Timeout = 10000;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
for (int i = 0; i < postParaList.Count; i++)
{
PostDataClass temp = postParaList[i];
// 写入字符串的Key
var stringKeyHeader = "Content-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
var header = string.Format(stringKeyHeader, temp.Prop, temp.Value);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);

if (i != postParaList.Count - 1)
memStream.Write(beginBoundary, 0, beginBoundary.Length);
else
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
}

byte[] b = memStream.ToArray();
string param = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
LogHelper.Info(string.Format("HttpClientHelper ==> PostMessage参数:{0}", param));

webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
using (HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream resStream = res.GetResponseStream())
{
byte[] buffer = new byte[1024];
int read;
while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
{
responseContent += Encoding.UTF8.GetString(buffer, 0, read);
}
}
res.Close();
}

LogHelper.Info(string.Format("HttpClientHelper ==> PostMessage返回值:{0}", responseContent));
return responseContent;
}
catch (Exception e)
{
LogHelper.Error(string.Format("HttpClientHelper ==> PostMessage方法======>Message:{0};StackTrace:{1};Source:{2};", e.Message, e.StackTrace, e.Source));
throw;
}
}
}

public class PostDataClass
{
public string Prop { get; set; }
public string Value { get; set; }
}