版權聲明:本文為博主原創文章,未經博主允許不得轉載。
在C#中有HttpWebRequest類,可以很方便用來獲取http請求,但是這個類對Post方式沒有提供一個很方便的方法來獲取數據。網上有很多人提供了解決方法,但都參差不齊,這里我把我使用的方法總結出來,與大家分享。
本文精華:實現了post的時候即可以有字符串的key-value,還可以帶文件。
Post數據格式
Post提交數據的時候最重要就是把Key-Value的數據放到http請求流中,而HttpWebRequest沒有提供一個屬性之類的東西可以讓我們自由添加Key-Value,因此就必須手工構造這個數據。
根據RFC 2045協議,一個Http Post的數據格式如下:
- Content-Type: multipart/form-data; boundary=AaB03x
- --AaB03x
- Content-Disposition: form-data; name="submit-name"
- Larry
- --AaB03x
- Content-Disposition: form-data; name="file"; filename="file1.dat"
- Content-Type: application/octet-stream
- ... contents of file1.dat ...
- --AaB03x--
詳細解釋如下:
Content-Type: multipart/form-data; boundary=AaB03x
如上面所示,首先聲明數據類型為multipart/form-data, 然后定義邊界字符串AaB03x,這個邊界字符串就是用來在下面來區分各個數據的,可以隨便定義,但是最好是用破折號等數據中一般不會出現的字符。然后是換行符。
注意:Post中定義的換行符是\r\n
--AaB03x
這個是邊界字符串,注意每一個邊界符前面都需要加2個連字符“--”,然后跟上換行符。
Content-Disposition: form-data; name="submit-name"
這里是Key-Value數據中字符串類型的數據。 submit-name 是這個Key-Value數據中的Key。當然也需要換行符。
Larry
這個就是剛才Key-Value數據中的value。
--AaB03x
邊界符,表示數據結束。
Content-Disposition: form-data; name="file"; filename="file1.dat"
這個代表另外一個數據,它的key是file,文件名是file1.dat。 注意:最后面沒有分號了。
Content-Type: application/octet-stream
這個標識文件類型。application/octet-stream表示二進制數據。
... contents of file1.dat ...
這個是文件內容。可以使二進制的數據。
--AaB03x--
數據結束后的分界符,注意因為這個后面沒有數據了所以需要在后面追加一個“--”表示結束。
C#下Post數據的函數
搞明白格式后,我們就很容易寫出C#的代碼了。如下所示:
- private static string HttpPostData(string url, int timeOut, string fileKeyName,
- string filePath, NameValueCollection stringDict)
- {
- string responseContent;
- var memStream = new MemoryStream();
- var webRequest = (HttpWebRequest)WebRequest.Create(url);
- // 邊界符
- var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
- // 邊界符
- var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
- var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- // 最后的結束符
- var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
- // 設置屬性
- webRequest.Method = "POST";
- webRequest.Timeout = timeOut;
- webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
- // 寫入文件
- const string filePartHeader =
- "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n"+
- "Content-Type: application/octet-stream\r\n\r\n";
- var header = string.Format(filePartHeader, fileKeyName, filePath);
- var headerbytes = Encoding.UTF8.GetBytes(header);
- memStream.Write(beginBoundary, 0, beginBoundary.Length);
- memStream.Write(headerbytes, 0, headerbytes.Length);
- var buffer = new byte[1024];
- int bytesRead; // =0
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- memStream.Write(buffer, 0, bytesRead);
- }
- var contentLine = Encoding.ASCII.GetBytes( "\r\n"); memStream.Write(contentLine, 0, contentLine.Length);
- // 寫入字符串的Key
- var stringKeyHeader = "\r\n--" + boundary +
- "\r\nContent-Disposition: form-data; name=\"{0}\""+
- "\r\n\r\n{1}\r\n";
- foreach (byte[] formitembytes in from string key in stringDict.Keys
- select string.Format(stringKeyHeader, key, stringDict[key])
- into formitem
- select Encoding.UTF8.GetBytes(formitem))
- {
- memStream.Write(formitembytes, 0, formitembytes.Length);
- }
- // 寫入最后的結束邊界符
- memStream.Write(endBoundary, 0, endBoundary.Length);
- 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();
- var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
- using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
- Encoding.GetEncoding("utf-8")))
- {
- responseContent = httpStreamReader.ReadToEnd();
- }
- fileStream.Close();
- httpWebResponse.Close();
- webRequest.Abort();
- return responseContent;
- }
參考資料