• HttpWebRequest 和HttpWebResponse总结


    1. 总结

    总结2

    3. Code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;
    using System.Web;
    
    namespace Core
    {
        public class RequestHelper
        {
            private Stream SendGetRequest1(string url)
            {
                string content;
                //HttpRequest request;//     Enables ASP.NET to read the HTTP values sent by a client during a Web request.
                WebRequest webrequest;//     Makes a request to a Uniform Resource Identifier (URI). This is an abstract
                HttpWebRequest httpWebRequest;//     Provides an HTTP-specific implementation of the System.Net.WebRequest class.
                HttpWebResponse httpWebResponse;
                //string url = "fsefsf";
                httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.Method = "GET";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    
                //httpwebrequest.ContentType = "application/json";
                //httpwebrequest.ContentType = "application/xml";
                //httpwebrequest.Headers.Add("url",url);
    
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                Stream resStream = httpWebResponse.GetResponseStream();
                using (StreamReader sr = new StreamReader(resStream))
                {
                    content = sr.ReadToEnd();
                }
                if (httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
    
                }
                return resStream;
            }
            private Stream SendGetRequestForStream(string url, string contentType)
            {
                HttpWebRequest httpWebRequest;//     Provides an HTTP-specific implementation of the System.Net.WebRequest class.
                HttpWebResponse httpWebResponse;
                httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.Method = "GET";
                httpWebRequest.ContentType = contentType;
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                Stream resStream = httpWebResponse.GetResponseStream();
                return resStream;
            }
    
    
            private String SendGetRequest(string url, string contentType)
            {
                string content = "";
                HttpWebRequest httpWebRequest;
                HttpWebResponse httpWebResponse;
                httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.Method = "GET";
                httpWebRequest.ContentType = contentType;//"application/x-www-form-encoded";
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                Stream resStream = httpWebResponse.GetResponseStream();
                using (StreamReader sr = new StreamReader(resStream))
                {
                    content = sr.ReadToEnd();
                }
                return content;
            }
    
            private Stream SendPostRequestForStream(string url, string data, string contentType)
            {
                //string content = "";
                HttpWebRequest httpWebRequest;
                HttpWebResponse httpWebResponse;
                httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = contentType;
                Stream reqStream = httpWebRequest.GetRequestStream();
                using (StreamWriter sw = new StreamWriter(reqStream))
                {
                    sw.Write(data);
                }
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                Stream ResStream = httpWebResponse.GetResponseStream();
    
                return ResStream;
            }
    
    
            private String SendPostRequest(string url, string data, string contentType)
            {
                string content = "";
                HttpWebRequest httpWebRequest;
                HttpWebResponse httpWebResponse;
                httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = contentType;
                Stream reqStream = httpWebRequest.GetRequestStream();
                using (StreamWriter sw = new StreamWriter(reqStream))
                {
                    sw.Write(data);
                }
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                Stream resStream = httpWebResponse.GetResponseStream();
                using (StreamReader sr = new StreamReader(resStream))
                {
                    content = sr.ReadToEnd();
                }
    
                return content;
            }
        }
    }
    View Code
  • 相关阅读:
    大batch任务对structured streaming任务影响
    spark 集群优化
    linux神器 strace解析
    打个 hadoop RPC的栗子
    netty 入门
    c#硬件对接数值转换
    RabbitMQ 消息队列入门
    RabbitMQ 开发环境安装部署
    Nginx-4.Nginx如何处理请求
    Nginx-3.控制nginx
  • 原文地址:https://www.cnblogs.com/slu182/p/4435573.html
Copyright © 2020-2023  润新知