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)

     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;
           }
    HttpGet
     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;
           }
    HttpPost
  • 相关阅读:
    android adb
    5 个免费的受欢迎的 SQLite 管理工具
    [Android]通过setImageURI设置网络上面的图片
    Android TextView实现长按复制文本功能的方法
    View工作原理(四)view的layout过程
    Anaroid WebView详解大全
    Android 如何在Eclipse中查看Android API源码以及support包源码
    关于Android的.so文件你所需要知道的
    AS问题解决系列3—iCCP: Not recognizing known sRGB profile(转)
    安卓App设计博文
  • 原文地址:https://www.cnblogs.com/kaikaichao/p/5912806.html
Copyright © 2011-2022 走看看