C#怎么利用http協(xié)議與服務(wù)器通信-創(chuàng)新互聯(lián)

這篇文章主要講解了“C#怎么利用http協(xié)議與服務(wù)器通信”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#怎么利用http協(xié)議與服務(wù)器通信”吧!

成都創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元綏陽做網(wǎng)站,已為上家服務(wù),為綏陽各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

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

{

    /// <summary>

    /// C#與http服務(wù)器端進(jìn)行對接的工具類

    /// </summary>

    class HttpWebTool

    {

        /// <summary>

        /// 用于緩存服務(wù)器端傳輸?shù)娇蛻舳说腟ESSIONID或者JSESSIONID

        /// </summary>

        private Cookie sessionidCookie = null;

        /// <summary>

        /// 從HttpWebServer端獲取數(shù)據(jù)(使用的是"post"方式)

        /// </summary>

        /// <param name="url">請求網(wǎng)址</param>

        /// <param name="data">請求參數(shù)集合,無需參數(shù)時傳入null值</param>

        /// <param name="cookies">請求cookie集合,無需cookie時傳入null值</param>

        /// <returns>返回請求結(jié)果字符串,返回為null代表請求失敗</returns>

        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)出鍵值對數(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";

                //聲明客戶端只接收txt類型的內(nèi)容

                req.Accept = "text/plain";

                //以鍵值對形式向服務(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()) {

                        //寫入請求實體流

                        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("請查看傳入?yún)?shù)是否正確或者服務(wù)器是否關(guān)閉");

            }

            return result;

        }

        /// <summary>

        /// 獲得參數(shù)data的消息數(shù)據(jù)流,以"\r\n"結(jié)尾

        /// </summary>

        /// <param name="data">請求參數(shù)集合,無需參數(shù)時傳入null值</param>

        /// <param name="boundary">消息分隔符</param>

        /// <returns>返回參數(shù)data的數(shù)據(jù)流,返回為空代表獲得失敗</returns>

        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;

        }

        /// <summary>

        /// 獲得上傳文件的消息頭部分字符流,以"\r\n\r\n"結(jié)尾

        /// </summary>

        /// <param name="de">上傳文件《控件名,上傳文件的保存位置(包括"文件名"."擴(kuò)展名")》</param>

        /// <param name="boundary">消息分隔符</param>

        /// <returns>返回上傳文件的消息頭部分字符流,返回會為null代表獲得失敗</returns>

        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;

        }

    }

}

感謝各位的閱讀,以上就是“C#怎么利用http協(xié)議與服務(wù)器通信”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C#怎么利用http協(xié)議與服務(wù)器通信這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

分享名稱:C#怎么利用http協(xié)議與服務(wù)器通信-創(chuàng)新互聯(lián)
本文鏈接:http://m.kartarina.com/article20/cdcdjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司云服務(wù)器App設(shè)計網(wǎng)站設(shè)計網(wǎng)站策劃面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
主站蜘蛛池模板: 精品人妻大屁股白浆无码| 国产人成无码视频在线观看| 13小箩利洗澡无码视频网站免费| 蜜臀亚洲AV无码精品国产午夜. | 亚洲精品无码专区2| 亚洲AV无码不卡在线播放| 无码中文在线二区免费| 国产AV无码专区亚洲AV漫画| 亚洲AV无码一区二区三区性色| 中文人妻无码一区二区三区 | 性色AV一区二区三区无码| 久久久精品人妻无码专区不卡| 久久精品亚洲中文字幕无码麻豆| 久久午夜无码鲁丝片午夜精品| 无码人妻精品中文字幕免费东京热| 中文字幕精品无码一区二区| 亚洲精品久久无码| 亚洲成av人片不卡无码| 国产品无码一区二区三区在线蜜桃 | 亚洲精品无码永久在线观看| 亚洲精品无码人妻无码| 免费A级毛片无码A∨免费| gogo少妇无码肉肉视频| 亚洲AV综合永久无码精品天堂| 乱色精品无码一区二区国产盗| 免费无码黄网站在线看| 东京热加勒比无码视频| 精品无码中文视频在线观看 | 无码av无码天堂资源网| 人妻av无码一区二区三区| 亚洲av日韩av无码黑人| 亚洲中文字幕久久精品无码APP| 惠民福利中文字幕人妻无码乱精品| 亚洲AV无码专区亚洲AV桃| 在线看片福利无码网址| 亚洲av无码有乱码在线观看| 亚洲GV天堂GV无码男同| 曰韩无码AV片免费播放不卡| 久青草无码视频在线观看| 国模无码视频一区二区三区| 亚洲av无码成人精品区在线播放|