using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace www.xinduofen.com
{
///
/// C#與http服務(wù)器端進(jìn)行對接的工具類
///
classwww.xinduofen.cn
{
///
/// 用于緩存服務(wù)器端傳輸?shù)娇蛻舳说腟ESSIONID或者JSESSIONID
///
private Cookie sessionidCookie = null;
///
/// 從HttpWebServer端獲取文件(使用的是"post"方式)
///
/// 請求網(wǎng)址
/// 請求參數(shù)集合,無需參數(shù)時(shí)傳入null值
/// 請求cookie集合神汹,無需cookie時(shí)傳入null值
/// 下載文件將要保存的位置(包括"文件名"."擴(kuò)展名")
/// 返回true代表成功濒持,false代表失敗
public Boolean getFileFromHttpWebServer(String url, Hashtable data, CookieCollection cookies,String fileSaveAddress)
{
Boolean result = false;
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(fileSaveAddress))
{
return false;//傳入?yún)?shù)異常
}
byte[] data_stream = null;//將要向服務(wù)器傳輸?shù)臄?shù)據(jù)流內(nèi)容
if (data != null && data.Count > 0)
{
string transportData = "";//將要向服務(wù)器傳輸?shù)淖址畠?nèi)容
foreach (DictionaryEntry de in data)
{
transportData = transportData + de.Key.ToString() + "=" + de.Value.ToString() + "&";//解調(diào)出鍵值對數(shù)據(jù)
}
transportData = transportData.TrimEnd('&');//去除字符串尾部的 &
if (!string.IsNullOrEmpty(transportData))
{
data_stream = Encoding.UTF8.GetBytes(transportData);//將上傳字符串?dāng)?shù)據(jù)打包成數(shù)據(jù)流
}
}
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
//請求方式
req.Method = "POST";
//聲明客戶端接收任意類型的文件流
req.Accept = "application/octet-stream";
//以鍵值對形式向服務(wù)器傳遞參數(shù)
req.ContentType = "application/x-www-form-urlencoded";
//設(shè)置cookie盒子(客戶端請求的cookie和服務(wù)器端返回的cookie就放在此盒子中)
CookieContainer cookieContainer = new CookieContainer();
if (sessionidCookie != null && !string.IsNullOrEmpty(sessionidCookie.Domain))
{
cookieContainer.Add(sessionidCookie);
}
if (cookies != null)
{
cookieContainer.Add(cookies);//添加調(diào)用者傳入的cookie集合
}
req.CookieContainer = cookieContainer;
if (data_stream != null && data_stream.Length > 0)
{
//請求數(shù)據(jù)流的長度
req.ContentLength = data_stream.Length;
using (Stream requestStream = req.GetRequestStream())
{
//寫入請求實(shí)體流
requestStream.Write(data_stream, 0, data_stream.Length);
}
}
//接收返回值
using (HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse()) {
if (myResponse.Cookies["SESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["SESSIONID"];
}
else
{
if (myResponse.Cookies["JSESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["JSESSIONID"];
}
}
////下面為服務(wù)器端返回的下載文件名,留下作為參考用的
//if (myResponse.StatusCode == HttpStatusCode.OK)
//{
// ? ?byte[] str = Encoding.GetEncoding("ISO-8859-1").GetBytes(myResponse.GetResponseHeader("Content-Disposition"));
// ? ?string disposition = Encoding.UTF8.GetString(str);
// ? ?Console.WriteLine("disposition:" + disposition);
//}
//流對象使用完后自動(dòng)關(guān)閉
using (Stream stream = myResponse.GetResponseStream())
{
//文件流北发,流信息讀到文件流中吐咳,讀完關(guān)閉
using (FileStream fs = File.Create(fileSaveAddress))
{
//建立字節(jié)組练湿,并設(shè)置它的大小是多少字節(jié)
byte[] bytes = new byte[10240];
int n = -1;
while ((n = stream.Read(bytes, 0, bytes.Length)) > 0)
{
fs.Write(bytes, 0, n); //將指定字節(jié)的流信息寫入文件流中
}
}
}
}
result = true;//下載成功
}
catch (Exception)
{
Console.WriteLine("請查看傳入?yún)?shù)是否正確或者服務(wù)器是否關(guān)閉");
}
return result;
}
///
/// 獲得參數(shù)data的消息數(shù)據(jù)流虽风,以"\r\n"結(jié)尾
///
/// 請求參數(shù)集合许溅,無需參數(shù)時(shí)傳入null值
/// 消息分隔符
/// 返回參數(shù)data的數(shù)據(jù)流温鸽,返回為空代表獲得失敗
private byte[] getParameterBytes(Hashtable data, String boundary)
{
byte[] parameterBytes = null;
//如果有請求參數(shù)
if (data != null && data.Count > 0)
{
string parameterStr = "";
foreach (DictionaryEntry de in data)
{
parameterStr += "--" + boundary;
parameterStr += "\r\n" + "Content-Disposition: form-data;name=\"" + de.Key.ToString() + "\"";
parameterStr += "\r\n" + "Content-Type: text/plain; charset=UTF-8";
parameterStr += "\r\n\r\n" + de.Value.ToString();
parameterStr += "\r\n";
}
if (!string.IsNullOrEmpty(parameterStr))
{
parameterBytes = Encoding.UTF8.GetBytes(parameterStr);//將上傳字符串?dāng)?shù)據(jù)打包成數(shù)據(jù)流
}
}
return parameterBytes;
}
///
/// 獲得上傳文件的消息頭部分字符流保屯,以"\r\n\r\n"結(jié)尾
///
/// 上傳文件《控件名,上傳文件的保存位置(包括"文件名"."擴(kuò)展名")》
/// 消息分隔符
/// 返回上傳文件的消息頭部分字符流手负,返回會(huì)為null代表獲得失敗
private byte[] getUploadFileDeclareBytes(DictionaryEntry de, String boundary)
{
byte[] uploadFileDeclareBytes = null;
//上傳文件的消息頭描述部分
string uploadFileDeclareStr = "";
uploadFileDeclareStr += "--" + boundary;
uploadFileDeclareStr += "\r\n" + "Content-Disposition: form-data;name=\"" + de.Key.ToString() + "\"; filename=\"" + de.Value.ToString() + "\"";
uploadFileDeclareStr += "\r\n" + "Content-Type: application/octet-stream";
uploadFileDeclareStr += "\r\n\r\n";
if (!string.IsNullOrEmpty(uploadFileDeclareStr))
{
uploadFileDeclareBytes = Encoding.UTF8.GetBytes(uploadFileDeclareStr);//將上傳字符串?dāng)?shù)據(jù)打包成數(shù)據(jù)流
}
return uploadFileDeclareBytes;
}
}
}
內(nèi)容所有權(quán)屬于越康體育(專業(yè)從事體質(zhì)測試儀,學(xué)生體質(zhì)測試儀)