• 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
    
    
    
     
  • 相关阅读:
    java程序员裸机配置
    安装库
    自定义脚本模板
    Oracle数据库触发器简单案例
    Oracle数据库按正则切割字符串
    Oracle查询一张表的所有字段
    Oracle数据库系统表
    Oracle设置最大连接数
    Oracle博客参考教程
    区间dp [H
  • 原文地址:https://www.cnblogs.com/musexiaoluo/p/7148528.html
Copyright © 2020-2023  润新知