zoukankan      html  css  js  c++  java
  • winform调用http

    一、post请求实现  

    
    
    public static string PostHttp(string url, string jsonParmaData)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                try
                {
                    
                    NetworkCredential auth = new NetworkCredential("admin", "www.hd123.com");//添加此代码
    
                    var postData = Encoding.UTF8.GetBytes(jsonParmaData);
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method = "POST";
                    httpWebRequest.Timeout = 10000;
                    httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
                    httpWebRequest.Credentials = auth;//添加认证
    
                    httpWebRequest.ContentLength = postData.Length;
                    httpWebRequest.GetRequestStream().Write(postData, 0, postData.Length);
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                  
     using (Stream stream = httpWebResponse.GetResponseStream()) 
    { 
    
    
    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) 
    { string str = sr.ReadToEnd(); 
    sr.Close(); 
    stream.Close(); 
    httpWebResponse.Close(); 
    return str;
     }
    
    }
     
    } catch (Exception ex)
     {
       try { httpWebRequest.Abort(); } catch { } 
    } 
    return null;
    
     } 
     
    View Code
    
    
    
     
  • 相关阅读:
    tornado中form表单验证详解
    关于tornado中session的总结
    Linux常用命令
    css3动画属性详解 与超酷例子
    keepalive高可用的健康检查
    keepalive的nginx防火墙问题
    安装配置hadoop
    tmux的简单快捷键
    部署使用elk
    k8s搭建部署
  • 原文地址:https://www.cnblogs.com/musexiaoluo/p/7148528.html
Copyright © 2011-2022 走看看