zoukankan      html  css  js  c++  java
  • 在线运行.NET代码

    https://dotnetfiddle.net/

    https://try.dot.net/

    C# 发送Http协议 模拟 Post Get请求

    1.参数 paramsValue的格式 要和 Reques.ContentType一致,

    如果 contentype  "application/x-www-form-urlencoded" 表单类型,那么  参数为   a=1&b=2 形式

    如果 。。。         "application/json"  json 类型  那么参数就为  "{a:1,b:2}" 格式

    2.可以添加自定义header,  add(key,value)

    接受获取header   Request.Headers.Get(key)

    复制代码
     public static string HttpGet(string url)
           { 
               string result=string.Empty;
               try
               {
                   HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                   wbRequest.Method = "GET";
                   HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                   using (Stream responseStream = wbResponse.GetResponseStream())
                   {
                       using (StreamReader sReader = new StreamReader(responseStream))
                       {
                           result = sReader.ReadToEnd();
                       }
                   }
               }
               catch (Exception ex)
               { 
               
               }
               return result;
           }
    复制代码
    复制代码
     public static string HttpPost(string url, string paramData, Dictionary<string, string> headerDic = null)
           {
               string result = string.Empty;
               try
               {
                   HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                   wbRequest.Method = "POST";
                   wbRequest.ContentType = "application/x-www-form-urlencoded";
                   wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
                   if (headerDic != null && headerDic.Count > 0)
                   {
                       foreach (var item in headerDic)
                       {
                           wbRequest.Headers.Add(item.Key, item.Value);
                       }
                   }
                   using (Stream requestStream = wbRequest.GetRequestStream())
                   {
                       using (StreamWriter swrite = new StreamWriter(requestStream))
                       {
                           swrite.Write(paramData);
                       }
                   }
                   HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                   using (Stream responseStream = wbResponse.GetResponseStream())
                   {
                       using (StreamReader sread = new StreamReader(responseStream))
                       {
                           result = sread.ReadToEnd();
                       }
                   }
               }
               catch (Exception ex)
               { }
             
               return result;
           }
    复制代码
  • 相关阅读:
    java邮箱发送
    mybaties xml 的头部
    eclipse启动报.log错误
    【高性能网站搭建-learn-web-vitals翻译】——Web Vitals
    【高性能网站搭建-learn-web-vitals翻译】——开篇
    git提交代码步骤以及工作中常用的git命令
    苹果手机focus没有效果 键盘跳不出来
    MVC,MVP与MVVM
    媒体查询用法及常见媒体尺寸
    浏览器内核
  • 原文地址:https://www.cnblogs.com/linmilove/p/10272953.html
Copyright © 2011-2022 走看看