网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#怎么利用http协议与服务器通信-创新互联

这篇文章主要讲解了“C#怎么利用http协议与服务器通信”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么利用http协议与服务器通信”吧!

成都创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元绥阳做网站,已为上家服务,为绥阳各地企业和个人服务,联系电话: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

{

    ///

    /// C#与http服务器端进行对接的工具类

    ///

    class HttpWebTool

    {

        ///

        /// 用于缓存服务器端传输到客户端的SESSIONID或者JSESSIONID

        ///

        private Cookie sessionidCookie = null;

        ///

        /// 从HttpWebServer端获取数据(使用的是"post"方式)

        ///

        /// 请求网址

        /// 请求参数集合,无需参数时传入null值

        /// 请求cookie集合,无需cookie时传入null值

        /// 返回请求结果字符串,返回为null代表请求失败

        public String getDatafromHttpWebServer(String url, Hashtable data,CookieCollection cookies)

        {

            String result = null;

            if (string.IsNullOrEmpty(url))

            {

                return null;//传入参数异常

            }

            byte[] data_stream = null;//将要向服务器传输的数据流内容

            if (data != null && data.Count > 0)

            {

                string transportData = "";//将要向服务器传输的字符串内容

                foreach (DictionaryEntry de in data)

                {

                    transportData = transportData + de.Key.ToString() + "=" + de.Value.ToString() + "&";//解调出键值对数据

                }

                transportData = transportData.TrimEnd('&');//去除字符串尾部的 &

                if (!string.IsNullOrEmpty(transportData))

                {

                    data_stream = Encoding.UTF8.GetBytes(transportData);//将上传字符串数据打包成数据流

                }

            }

            try

            {

                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);

                //请求方式

                req.Method = "POST";

                //声明客户端只接收txt类型的内容

                req.Accept = "text/plain";

                //以键值对形式向服务器传递参数

                req.ContentType = "application/x-www-form-urlencoded";

                //设置cookie盒子(客户端请求的cookie和服务器端返回的cookie就放在此盒子中)

                CookieContainer cookieContainer = new CookieContainer();

                if (sessionidCookie != null && !string.IsNullOrEmpty(sessionidCookie.Domain))

                {

                    cookieContainer.Add(sessionidCookie);

                }

                if (cookies!=null)

                {

                    cookieContainer.Add(cookies);//添加调用者传入的cookie集合

                }

                req.CookieContainer = cookieContainer;

                if (data_stream != null && data_stream.Length > 0)

                {

                    //请求数据流的长度

                    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("请查看传入参数是否正确或者服务器是否关闭");

            }

            return result;

        }

        ///

        /// 获得参数data的消息数据流,以"\r\n"结尾

        ///

        /// 请求参数集合,无需参数时传入null值

        /// 消息分隔符

        /// 返回参数data的数据流,返回为空代表获得失败

        private byte[] getParameterBytes(Hashtable data, String boundary)

        {

            byte[] parameterBytes = null;

            //如果有请求参数

            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);//将上传字符串数据打包成数据流

                }

            }

            return parameterBytes;

        }

        ///

        /// 获得上传文件的消息头部分字符流,以"\r\n\r\n"结尾

        ///

        /// 上传文件《控件名,上传文件的保存位置(包括"文件名"."扩展名")》

        /// 消息分隔符

        /// 返回上传文件的消息头部分字符流,返回会为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);//将上传字符串数据打包成数据流

            }

            return uploadFileDeclareBytes;

        }

    }

}

感谢各位的阅读,以上就是“C#怎么利用http协议与服务器通信”的内容了,经过本文的学习后,相信大家对C#怎么利用http协议与服务器通信这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页标题:C#怎么利用http协议与服务器通信-创新互联
文章链接:http://bjjierui.cn/article/cdcdjo.html

其他资讯