zoukankan      html  css  js  c++  java
  • C#访问HTTP请求

    场景描述:在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

  • 相关阅读:
    网易云课堂Dubbo学习笔记
    Java的native方法
    java中三种for循环之间的对比
    java中的匿名内部类小结
    三重DEC加密在java中的实现
    CoreException: Could not get the value for parameter compilerId for plugin execution default-compile Maven项目pom文件报错,插件引用不到
    安装plsql developer
    Eclipse安装插件的“最好方法”:dropins文件夹的妙用
    linux项目部署常用命令
    Linux学习笔记
  • 原文地址:https://www.cnblogs.com/ggll611928/p/15711385.html
Copyright © 2011-2022 走看看