永不言弃!
不忘初心:感恩的心!上进的心!

场景描述:在cs系统中,调用http服务端的接口

因为cs中没有自带的http请求方法,所以需要用到system.net

 

get方式

        /// <summary>
        /// GET
        /// </summary>
        public void GetHttpData()
        {   
            string uri = "http://www.baidu.com";

            //创建HttpWebRequest请求,设置请求报文信息
            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; 
            request.ContentType = "application/json"; 
            request.Method = "GET";                                 //请求方法
            //request.ProtocolVersion = new Version(1, 1);        //Http/1.1版本
            
            //接收响应,输出响应头部信息以及主体信息 
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            #region 接收响应头部信息
            foreach (var item in response.Headers)
            {
                this.rtbData.Text += item.ToString() + ": " +
                response.GetResponseHeader(item.ToString()) + "\n"; 
            }
            #endregion

            #region 接收响应主体信息 
            string content = "";
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                {
                    content = sr.ReadToEnd();
                    sr.Close();
                    stream.Close();
                    response.Close(); 
                } 
            }
            #endregion

            //将接收到的数据显示到界面 
            this.rtbData.Text += content; 
        }
View Code

 

post方式

        /// <summary>
        /// POST
        /// </summary>
        public void PostHttp()
        {
            string uri = "http://www.baidu.com";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            NetworkCredential auth = new NetworkCredential("admin", uri);       
            string jsonParmaData = @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}";
            var postData = Encoding.UTF8.GetBytes(jsonParmaData);   //传递的参数

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 10000;
            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
            //httpWebRequest.Credentials = auth;  //添加认证
             
            httpWebRequest.ContentLength = postData.Length;
            httpWebRequest.GetRequestStream().Write(postData, 0, postData.Length);
            httpWebRequest.GetRequestStream().Close();

            string str = "";
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (Stream stream = httpWebResponse.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                {
                    str = sr.ReadToEnd();
                    sr.Close();
                    stream.Close();
                    httpWebResponse.Close();
                } 
            }
            //将获取的数据显示在界面中
            this.rtbData.Text = str; 
        }
View Code

 

 

本文引自:https://www.dandelioncloud.cn/article/details/1408092008732651521

posted on 2021-12-20 16:11  永不言弃!  阅读(408)  评论(0编辑  收藏  举报