注意,数据内容需要使用WebUtility.UrlEncode编码,否则会出现Https接口错误

使用WebClient

public class WebClientEx: WebClient
	public int Timeout{get;set;}
	protected override WebRequest GetWebRequest(Url address)
		var request = base.GetWebRequest(address);
		request.Timeout = Timeout;
		return request;
static public void PushTest()
    string PostUrl = ConfigurationManager.AppSettings["PostTest"];
    if(PostUrl == "")
        Console.WriteLine("请配置PostUrl");
        return;
    Console.WriteLine("PostUrl="+ PostUrl);
    string PostDelay = ConfigurationManager.AppSettings["PostDelay"]??"3";
    int nDelay = int.Parse(PostDelay);
    Console.WriteLine("PostDelay=" + nDelay);
    string strData = @"{
    ""name"":""hallo"",
    ""id"":123456,
    ""thetime"":""2017/6/20 21:28:44""
    strData = "data="+WebUtility.UrlEncode(strData);
    if (PostUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    //ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;// (SecurityProtocolType)3072 for .net4;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    //System.Net.WebClient wc = new WebClient();
    WebClientEx wc = new WebClientEx();
    wc.Timeout = nDelay*1000;
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//可以post提交
    wc.Encoding = Encoding.UTF8;
    //string[] pinfo = ConfigurationManager.AppSettings["EnableProxy"].Split('|');
    //if (pinfo[0].ToLower() == "true")
    //	wc.Proxy = new WebProxy(pinfo[1], int.Parse(pinfo[2]));
    string result = "";
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
        result = wc.UploadString(PostUrl, "POST", strData);
        sw.Stop();
        Console.WriteLine("Fun0 Ret=" + result);
        Console.WriteLine("耗时=" + sw.ElapsedMilliseconds+" 毫秒");
    catch (System.Exception ex)
        sw.Stop();
        Console.WriteLine("Fun0 Exception="  + ex.Message);
        Console.WriteLine("耗时=" + sw.ElapsedMilliseconds + " 毫秒");

使用HttpWebRequest

public static void PushTest2()
    string url = ConfigurationManager.AppSettings["PostTest"];
    string body = @"{
        ""name"":""TEST"",
        ""id"":123456,
        ""thetime"":""2017/6/20 21:28:44""
    IDictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("data", body);
    //HTTPSQ请求
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    Encoding charset = Encoding.UTF8;
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    //request.ProtocolVersion = HttpVersion.Version11;
    //request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";
    //request.KeepAlive = true;
    //request.CookieContainer = new CookieContainer();
    //request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
    //request.Headers["token"] = "";
    //request.Headers["Cookie"] = "username=aaaaaa; Language=zh_CN";
    //request.Accept = "text/html, application/xhtml+xml, */*";
    if (!(parameters == null || parameters.Count == 0))
        StringBuilder buffer = new StringBuilder();
        int i = 0;
        foreach (string key in parameters.Keys)
            if (i > 0)
                buffer.AppendFormat("&{0}={1}", key, WebUtility.UrlEncode(parameters[key]));
                buffer.AppendFormat("{0}={1}", key, WebUtility.UrlEncode(parameters[key]));
        byte[] data = charset.GetBytes(buffer.ToString());
        using (Stream stream = request.GetRequestStream())
            stream.Write(data, 0, data.Length);
        HttpWebResponse response =  (HttpWebResponse)request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            Console.WriteLine("Fun2 Ret=" + reader.ReadToEnd());
    catch (System.Exception ex)
        Console.WriteLine("Fun2 Exception=" + ex.Message);
    string result = "";
    System.Net.HttpWebRequest req = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
    if (url.StartsWith("https", StringComparison.Or
A(对方公司)提供接口服务,地址时http前缀的,加上用户名和密码等数据后测试时没有问题,可以直接调通,但在切换到生产后,地址变更为https前缀,然后这个接口就调不到。
产生问题分析:
https前缀会有ssl证书验证,在post调取该地址时,可以忽略掉该验证,否则会产生调不到的情况。
解决办法:
代码调用时需在调用地址前加忽略掉ssl验证这段代码,如下:
ServicePointManager.ServerCertificateValidationCallback = (sender, 
                                    到底是学艺不精哈。最近有点流年不利。总是遇到莫名其妙的坑。https发送post请求,死活返回的都是500(内部服务器错误)。郁闷ing。代码调试也没有提示啥有效的错误提示。用抓包工具也看不出任何异常。只有postman能调通,postman是何方神圣?为啥总能调通。期初还以为是证书之类的问题,在代码中添加了证书之类的东西也还是不行,依然不能解决问题。在google上查了半天,没有一个人遇到过我的...
public  string HttpPost1(string url, string body)
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //一定要有这一句
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (Ht...
                                    介绍http请求的两种方式,get和post方式。并用C#语言实现,如何请求url并获取返回的数据两者的区别:
参数Get请求把提交的数据进行简单编码,同时将url的一部分发送到服务器 
 比如url:Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB 
 所以get请求方式提交的数据存在一定的安全隐患,如果在使用对
                                    我是通过VS2010 ,新建一个winform窗体项目,然后写了一个测试软件,软件里最后通过HTTP的POST把测试结果数据上传到一个网页系统里,我们之间的协议很简单: 
C#这边就是标准的POST发送格式(网页系统服务器地址+端口号+具体路径+一个问号+数据字段名=数据值 +&amp;+数据字段名=数据值………) 
网页系统那边返回jason格式对象(左大括号{ + 双引号里字段名 + 引号...
                                    WebClient:
1.post字符串
  public string SendField_wc(string URL,string CookieData, string postData)
            WebClient myWebClient = new WebClient();
            myWebClient.Headers.A...