• HttpRequest Get Post,WebClient Get


            #region HttpRequestGet
            public string HttpRequestGet(string url)
            {
                return HttpRequestGet(url, WebRequestMethods.Http.Get, "");
            }
            public string HttpRequestPost(string url, string data)
            {
                return HttpRequestGet(url, WebRequestMethods.Http.Post, data);
            }
            private string HttpRequestGet(string url, string method, string data)
            {
                var request = WebRequest.Create(url);
                request.Method = method;
    
                if (method == WebRequestMethods.Http.Post)
                {
                    byte[] buffer = Encoding.Default.GetBytes(data);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = buffer.Length;
    
                    var streamRequest = request.GetRequestStream();
                    streamRequest.Write(buffer, 0, buffer.Length);
                    streamRequest.Close();
                }
    
                var response = request.GetResponse();
                var streamResponse = response.GetResponseStream();
                if (streamResponse == null) return "";
                var reader = new StreamReader(streamResponse, Encoding.Default);
                var result = reader.ReadToEnd();
    
                streamResponse.Close();
                reader.Close();
                return result;
            }
            #endregion
    
            public string WebClientGet(string url)
            {
                var client = new WebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                var stream = client.OpenRead(url);
                if (stream == null) return "";
                var reader = new StreamReader(stream, Encoding.Default);
                var result = reader.ReadToEnd();
                stream.Close();
                reader.Close();
                return result;
            }
  • 相关阅读:
    python 中的深拷贝和浅拷贝
    时间复杂度
    为什么l1和l2可以防止过拟合
    逻辑回归实现多分类
    《深入浅出数据分析》读书笔记
    牛客网的输入和输出
    万物皆可embedding
    NLP中的mask的作用
    不同语言对单例模式的不同实现
    ElasticSearch 索引 VS MySQL 索引
  • 原文地址:https://www.cnblogs.com/tearer/p/2759508.html
Copyright © 2020-2023  润新知