• 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;
            }
  • 相关阅读:
    svn的差异查看器和合并工具换成BCompare.exe
    Java经典编程题50道之十四
    Java经典编程题50道之十三
    Java经典编程题50道之十二
    Java经典编程题50道之十一
    Java经典编程题50道之十
    Java经典编程题50道之九
    Java经典编程题50道之八
    Java经典编程题50道之七
    Java经典编程题50道之六
  • 原文地址:https://www.cnblogs.com/wangzhe688/p/7809290.html
Copyright © 2020-2023  润新知