• 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

  • 相关阅读:
    声明:此资源由本博客收集整理于网络,只用于交流学习,请勿用作它途。如有侵权,请联系, 删除处理。
    注入点归纳
    网站入侵思路
    关于网上的“人肉”里面的技巧,简单解释
    SQL注入复习
    自己构造注入点方便入侵
    SQL通常注射的一些介绍
    AWVS13破解版安装_Windows
    黑页
    显ipQQ
  • 原文地址:https://www.cnblogs.com/ggll611928/p/15711385.html
Copyright © 2020-2023  润新知