內(nèi)容很好,可以直接使用,內(nèi)容所有權(quán)屬于:htpp://www.xinduofen.com/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace www.xinduofen.cn
{
///
/// C#與http服務(wù)器端進(jìn)行對(duì)接的工具類
///
class HttpWebTool
{
///
/// 用于緩存服務(wù)器端傳輸?shù)娇蛻舳说腟ESSIONID或者JSESSIONID
///
private Cookie sessionidCookie = null;
///
/// 從HttpWebServer端獲取數(shù)據(jù)(使用的是"post"方式)
///
/// 請(qǐng)求網(wǎng)址
/// 請(qǐng)求參數(shù)集合,無需參數(shù)時(shí)傳入null值
/// 請(qǐng)求cookie集合,無需cookie時(shí)傳入null值
/// 返回請(qǐng)求結(jié)果字符串,返回為null代表請(qǐng)求失敗
public String getDatafromHttpWebServer(String url, Hashtable data,CookieCollection cookies)
{
String result = null;
if (string.IsNullOrEmpty(url))
{
return null;//傳入?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)出鍵值對(duì)數(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);
//請(qǐng)求方式
req.Method = "POST";
//聲明客戶端只接收txt類型的內(nèi)容
req.Accept = "text/plain";
//以鍵值對(duì)形式向服務(wù)器傳遞參數(shù)
req.ContentType = "application/x-www-form-urlencoded";
//設(shè)置cookie盒子(客戶端請(qǐng)求的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)
{
//請(qǐng)求數(shù)據(jù)流的長(zhǎng)度
req.ContentLength = data_stream.Length;
using (Stream requestStream = req.GetRequestStream()) {
//寫入請(qǐng)求實(shí)體流
requestStream.Write(data_stream, 0, data_stream.Length);
}
}
//接收返回值
using(HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse()){
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd().Trim();
}
if (myResponse.Cookies["SESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["SESSIONID"];
}
else
{
if (myResponse.Cookies["JSESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["JSESSIONID"];
}
}
}
}catch(Exception){
Console.WriteLine("請(qǐng)查看傳入?yún)?shù)是否正確或者服務(wù)器是否關(guān)閉");
}
return result;
}
///
/// 獲得參數(shù)data的消息數(shù)據(jù)流,以"\r\n"結(jié)尾
///
/// 請(qǐng)求參數(shù)集合,無需參數(shù)時(shí)傳入null值
/// 消息分隔符
/// 返回參數(shù)data的數(shù)據(jù)流,返回為空代表獲得失敗
private byte[] getParameterBytes(Hashtable data, String boundary)
{
byte[] parameterBytes = null;
//如果有請(qǐng)求參數(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ì)健康測(cè)試儀器、體質(zhì)測(cè)試儀)