C# HTTP或HTTPS Get请求 和 Post 请求


一、GET请求 C# HTTP或HTTPS GET请求URL 获得返回信息

private string GetUrl(string url)
{
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
    {
        ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
        request.ProtocolVersion = HttpVersion.Version11;
        // 这里设置了协议类型。
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
        request.KeepAlive = false;
        ServicePointManager.CheckCertificateRevocationList = true;
        ServicePointManager.DefaultConnectionLimit = 100;
        ServicePointManager.Expect100Continue = false;
    }
    request.Method = "GET";    //使用get方式发送数据
    request.ContentType = "application/x-www-form-urlencoded";
    request.Referer = null;
    request.AllowAutoRedirect = true;
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    request.Accept = "*/*";

    //获取网页响应结果
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string result = string.Empty;
    using (StreamReader sr = new StreamReader(stream))
    {
        result = sr.ReadToEnd();
    }

    return result;
}
GarsonZhang www.yesdotnet.com

二、POST请求 C# HTTP或HTTPS POST请求URL 获得返回信息

JSON格式

private static string PostUrl(string url, string postData)
{
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
    {
        ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
        request.ProtocolVersion = HttpVersion.Version11;
         // 这里设置了协议类型。
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
        request.KeepAlive = false;
        ServicePointManager.CheckCertificateRevocationList = true;
        ServicePointManager.DefaultConnectionLimit = 100;
        ServicePointManager.Expect100Continue = false;
    }
    request.Method = "POST";    //使用get方式发送数据
    request.ContentType = "application/x-www-form-urlencoded";
    request.Referer = null;
    request.AllowAutoRedirect = true;
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    request.Accept = "*/*";

    byte[] data = Encoding.UTF8.GetBytes(postData);
    Stream newStream = request.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    //获取网页响应结果
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string result = string.Empty;
    using (StreamReader sr = new StreamReader(stream))
    {
        result = sr.ReadToEnd();
    }

    return result;
}
GarsonZhang www.yesdotnet.com

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:TinyMCE自定义字体大小列表
下一篇:TinyMCE 支持的图标列表
评论列表

发表评论

评论内容
昵称:
关联文章

C# HTTPHTTPS Get请求 Post 请求
【HttpHelper】HTTP GetPost请求
.NET中大型项目开发必备(9)--http请求调用(PostGet)
Jquery请求API,AJax,Post,Get提交,失败,错误的处理
C#HTTP请求RestSharp.RestClient发起https请求报错
使用.NET 6开发TodoList应用(10)——实现DELETE请求以及HTTP请求幂等性
使用.NET 6开发TodoList应用(7)——使用AutoMapper实现GET请求
Http请求中Referer的设置,CEFSharp带Referer请求
使用.NET 6开发TodoList应用(6)——使用MediatR实现POST请求
C#利用CefSharp的ChromiumWebBrowser发起Post请求
RestSharp请求https添加Cookie信息的正确姿势
微信支付:Http头缺少AcceptUser-Agent
.net core MVC 使用 jquery ajax请求 Post json
解决 axios 跨域时,发送 post 请求前options 404
使用.NET 6开发TodoList应用(19)——处理OPTIONHEAD请求
Winform开启一个http服务,web服务
使用.NET 6开发TodoList应用(9)——实现PUT请求
C# 设置验证 PDF中的文本域格式
Python windows服务报错: 1053:服务没有及时响应启动控制请求
使用.NET 6开发TodoList应用(11)——使用FluentValidationMediatR实现接口请求验证

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码