• 【.NET】HttpWebRequest和HttpWebReponse的Get,Post请求


    支持https几种安全协议的

    /// <summary>
            /// Get请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="timeout"></param>
            /// <param name="encode"></param>
            /// <returns></returns>
            public string Request_Get(string url,int timeout, Encoding encode)
            {
                string result = "";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse response = null;
                try
                {
                    //设置ServicePoint实体
                    ServicePointManager.Expect100Continue = true;//是否通过100-continue协议,在post请求前,会先请求是否包含这个头域
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };//回调统一方法
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;//Protocol设置安全协议
    
                    //请求
                    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Timeout = timeout;
                    request.Proxy = null;
                    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0";
                    
                    //响应
                    response = (HttpWebResponse)request.GetResponse();
                    Stream stream = request.GetRequestStream();
                    StreamReader streamReader = new StreamReader(stream, encode);
                    return streamReader.ReadToEnd();
                }
                catch(Exception exc)
                {
                    return exc.Message;
                }
                finally
                {
                    if (response != null) { response.Close(); }
                    if (request != null) { request.Abort(); }
                }
            }
    
            public static string Request_Post(string url,string Param, int timeout, Encoding encode)
            {
                byte[] p = encode.GetBytes(Param);
    
                //请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = null;
                Stream stream = null;
                //request.KeepAlive = true;//关掉在线的连接,用close,Abort代替
                request.Method = "POST";
                //*/*/如果是直接打开需要
                request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*";
                //请求类型
                request.ContentType = "application/x-www-form-urlencoded";
                //客户浏览器信息
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MAXTHON 2.0)";
                //长度,gzip和default会是-1
                request.ContentLength = p.Length;
                
                //响应
                try
                {
                    //请求转成字节流,将参数写入,再返回响应给响应对象,按字节读出
                    stream = request.GetRequestStream();
                    stream.Write(p, 0, p.Length);
                    stream.Close();
                    response = (HttpWebResponse)request.GetResponse();
                    StreamReader streamReader = new StreamReader(response.GetResponseStream(), encode);
                    return streamReader.ReadToEnd();
                }
                catch(Exception exc)
                {
                    return exc.Message;
                }
            }
  • 相关阅读:
    【monkey测试】Fragment not attached to Activity
    android加载字体内存泄漏的处理方法
    性能测试—瓶颈分析方法
    EXT 环境部署
    测试用例设计
    软件测试职业规划
    mysql存储过程
    testlink安装
    并崩溃原因及解决方案
    MySQL正则表达式
  • 原文地址:https://www.cnblogs.com/laokchen/p/12514633.html
Copyright © 2020-2023  润新知