zoukankan      html  css  js  c++  java
  • HTTP (httpwebrequest)

    1.GET请求:

    public static string Get(string url)
            {
                string buffer = "";
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Proxy = new WebProxy();
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                    buffer = reader.ReadToEnd();
                    reader.Close();
                    response.Close();
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("网络异常");
                    return "";
                }
                return buffer;
            }

    2.POST请求:

    public static string Send(string url, byte[] data)
            {
                string buffer = "";
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Proxy = new WebProxy();
                    request.Timeout = 1000 * 600;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
    
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                    buffer = reader.ReadToEnd();
    
                    reader.Close();
                    response.Close();
    
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("网络异常");
                    return "";
                }
                return buffer;
            }
  • 相关阅读:
    demo_38 关注页导航栏实现
    demo_37 评论列表实现_02 封装popup 及 格式化时间
    demo_37 评论列表实现_01
    SaaS
    rsyncd脚本
    rsyncd
    MySQL高可用--MHA安装
    正向代理
    zabbix 一键部署
    kvm安装
  • 原文地址:https://www.cnblogs.com/wangzhe688/p/7809290.html
Copyright © 2011-2022 走看看