zoukankan      html  css  js  c++  java
  • 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)

     
     1 public static string HttpGet(string url)
     2        { 
     3            string result=string.Empty;
     4            try
     5            {
     6                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
     7                wbRequest.Method = "GET";
     8                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
     9                using (Stream responseStream = wbResponse.GetResponseStream())
    10                {
    11                    using (StreamReader sReader = new StreamReader(responseStream))
    12                    {
    13                        result = sReader.ReadToEnd();
    14                    }
    15                }
    16            }
    17            catch (Exception ex)
    18            { 
    19            
    20            }
    21            return result;
    22        }
    HttpGet
     1  public static string HttpPost(string url, string paramData, Dictionary<string, string> headerDic = null)
     2        {
     3            string result = string.Empty;
     4            try
     5            {
     6                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
     7                wbRequest.Method = "POST";
     8                wbRequest.ContentType = "application/x-www-form-urlencoded";
     9                wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
    10                if (headerDic != null && headerDic.Count > 0)
    11                {
    12                    foreach (var item in headerDic)
    13                    {
    14                        wbRequest.Headers.Add(item.Key, item.Value);
    15                    }
    16                }
    17                using (Stream requestStream = wbRequest.GetRequestStream())
    18                {
    19                    using (StreamWriter swrite = new StreamWriter(requestStream))
    20                    {
    21                        swrite.Write(paramData);
    22                    }
    23                }
    24                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
    25                using (Stream responseStream = wbResponse.GetResponseStream())
    26                {
    27                    using (StreamReader sread = new StreamReader(responseStream))
    28                    {
    29                        result = sread.ReadToEnd();
    30                    }
    31                }
    32            }
    33            catch (Exception ex)
    34            { }
    35          
    36            return result;
    37        }
    HttpPost
  • 相关阅读:
    oracle过期备份未删除导致磁盘撑爆
    数据文件、监听日志、告警日志、redo日志、归档日志的迁移
    linux软件卸载命令
    Nginx版本平滑升级方案
    rsync 服务搭建
    在node节点部署kubectl管理k8s集群
    源码编译安装nginx及设置开机启动项
    K8S日常运维中关于“ImagePullBackOff”报错的处理思路分析
    查看所有日志命令:journalctl
    Docker编排工具Docker Compose的使用
  • 原文地址:https://www.cnblogs.com/ZGQ-VIP/p/11753571.html
Copyright © 2011-2022 走看看