• C#get ,post HttpClient


    (1)get  

    发送url请求

    设置请求方式

    设置请求内容格式

    获得回应对象,接收流

    读取流

      private string Get(string url)
            {
                string all = string.Empty;
                //发送请求
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "get";//设置方法
                request.ContentType = "text/html;charset=UTF-8";
                //获得响应
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //读取流
                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                all= sr.ReadToEnd();
                //关闭流
                stream.Close();
                sr.Close();
                return all;
            }

    (2)post

    发送url请求

    设置请求方式

    设置请求内容格式

    设置请求内容长度(可以不设置)(长度是byte数组的长度,不是string类型的长度)

    将post数据添加到请求流中(重中之重GetRequestStream())

    接收回应对象,接收流

    读取流

     public static string Post(string postdata, string url)
            {
                HttpWebResponse hw;
                string result = string.Empty;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/json";
                #region 添加Post 参数  
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);//将post对象放入请求流中
                    reqStream.Close();
                }
                #endregion
                try
                {
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容  
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                catch (WebException w)
                {
                    hw = (HttpWebResponse)w.Response;
                    StreamReader sr = new StreamReader(hw.GetResponseStream(), Encoding.UTF8, false);
                    result = sr.ReadToEnd();
                }
                return result;
            }

     (2)HttpClient

     string meeturl = "http://www.baidu.cn/?title=";
                StringBuilder s = new StringBuilder();
                string url = meeturl + "m";
                HttpClient client = new HttpClient();
                client.Timeout = new TimeSpan(0,0,2);
                 HttpResponseMessage hm=  client.GetAsync(url).Result;//通过html可以获得返回的多个信息
                s.AppendLine("StatusCode:      "+hm.StatusCode);
                s.AppendLine("GetType().ToString():      " + hm.GetType().ToString());
                s.AppendLine(hm.Content.ReadAsStringAsync().Result);
                textBox1.Text = s.ToString();
  • 相关阅读:
    Plus One
    Move Zeroes
    Valid Number
    对于人生道路的些许感慨
    关于session.getValueNames()的一个问题
    jsp记录
    alter和modify的区别
    关于新手SQL Server 2008数据库的首次使用出现的问题
    今年大二,要开始java的学习了,听前辈说记录一下自己的学习过程会有一些好处
    MFC多文档无法显示可停靠窗格
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/6848953.html
Copyright © 2020-2023  润新知