博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# POST与Get数据
阅读量:5290 次
发布时间:2019-06-14

本文共 6702 字,大约阅读时间需要 22 分钟。

引用DLL

 

普通Get数据和Post数据

 

1    public static string Get(string URL) 2         { 3             String ReCode = string.Empty; 4             try 5             { 6                 HttpWebRequest wNetr = (HttpWebRequest)HttpWebRequest.Create(URL); 7                 HttpWebResponse wNetp = (HttpWebResponse)wNetr.GetResponse(); 8                 wNetr.ContentType = "text/html"; 9                 wNetr.Method = "Get";10                 Stream Streams = wNetp.GetResponseStream();11                 StreamReader Reads = new StreamReader(Streams, Encoding.UTF8);12                 ReCode = Reads.ReadToEnd();13 14                 //封闭临时不实用的资料 15                 Reads.Dispose();16                 Streams.Dispose();17                 wNetp.Close();18             }19             catch (Exception ex) { throw ex; }20 21             return ReCode;22 23         }24 25         public static string Post(string url, string data)26         {27             string returnData = null;28             try29             {30                 //byte[] buffer = Encoding.UTF8.GetBytes(data);31                 //HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);32                 //webReq.Method = "POST";33                 //webReq.ContentType = "application/x-www-form-urlencoded";34                 //webReq.ContentLength = buffer.Length;35                 //Stream postData = webReq.GetRequestStream();36                 //webReq.Timeout = 99999999;37                 ////webReq.ReadWriteTimeout = 99999999;38                 //postData.Write(buffer, 0, buffer.Length);39                 //postData.Close();40                 //HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();41                 //Stream answer = webResp.GetResponseStream();42                 //StreamReader answerData = new StreamReader(answer);43                 //returnData = answerData.ReadToEnd();44 45                 string strURL = url;46                 System.Net.HttpWebRequest request;47                 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);48                 request.Method = "POST";49                 request.ContentType = "application/json;charset=UTF-8";50                 string paraUrlCoded = data;51                 byte[] payload;52                 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);53                 request.ContentLength = payload.Length;54                 Stream writer = request.GetRequestStream();55                 writer.Write(payload, 0, payload.Length);56                 writer.Close();57                 System.Net.HttpWebResponse response;58                 response = (System.Net.HttpWebResponse)request.GetResponse();59                 System.IO.Stream s;60                 s = response.GetResponseStream();61                 string StrDate = "";62                 string strValue = "";63                 StreamReader Reader = new StreamReader(s, Encoding.UTF8);64                 while ((StrDate = Reader.ReadLine()) != null)65                 {66                     strValue += StrDate + "\r\n";67                 }68                 returnData = strValue;69             }70             catch71             {72                 return "获取错误";73             }74             return returnData.Trim() + "\n";75         }76         /// 77         /// 将json数据反序列化为Dictionary78         /// 79         /// json数据80         /// 
81 public static Dictionary
JsonToDictionary(string jsonData)82 {83 //实例化JavaScriptSerializer类的新实例。。。需要添加 System.Web.Extensions引用84 JavaScriptSerializer jss = new JavaScriptSerializer();85 try86 {87 //将指定的 JSON 字符串转换为 Dictionary
类型的对象88 return jss.Deserialize
>(jsonData);89 }90 catch (Exception ex)91 {92 throw new Exception(ex.Message);93 }94 }

附.另外一个Post

public void Post(){ var d = new SendNews();            d.touser = touser;            d.agentid = "7";            d.Description = wxmsg;            HttpClient client = new HttpClient();            client.BaseAddress = new Uri("http://localhost:47870/");            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));            HttpResponseMessage response = client.PostAsJsonAsync("api/SendMsg/SendNews", d).Result; }   public class SendNews         {            ///             /// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送             ///             public string touser { get; set; }            ///             /// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数            ///             public string toparty { get; set; }            ///             /// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数             ///             public string totag { get; set; }            ///             /// 消息类型            ///             public string msgtype { get; set; }            ///             /// 企业应用的id,整型。可在应用的设置页面查看             ///             public string agentid { get; set; }            ///             /// 表示是否是保密消息,0表示否,1表示是,默认0             ///             public string safe { get; set; }            ///             /// 文章标题            ///             public string Title { get; set; }            ///             /// 文章描述            ///             public string Description { get; set; }            ///             /// 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200            ///             public string PicUrl { get; set; }            ///             /// 点击图文消息跳转链接            ///             public string Url { get; set; }        }

 

键值对提交

//重点!! 成功 利用Webclient Post 按字段的方式提交每个字段的内容给服务器端         public string WebClientPost(string url, System.Collections.Specialized.NameValueCollection PostVars)        {            try            {                System.Net.WebClient WebClientObj = new System.Net.WebClient();                byte[] byRemoteInfo = WebClientObj.UploadValues(url, "POST", PostVars);                //下面都没用啦,就上面一句话就可以了                string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);                return sRemoteInfo;            }            catch            {                return "";            }        }        public void test()        {            System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();            PostVars.Add("test", "value");            PostVars.Add("cmd", "value");            PostVars.Add("token", "value");            WebClientPost("url", PostVars);        }

 

转载于:https://www.cnblogs.com/GarsonZhang/p/4566286.html

你可能感兴趣的文章
Mysql安装方法及安装问题解决
查看>>
Java动态代理的两种实现方式:
查看>>
PHP trait
查看>>
Redis的常用命令(三)
查看>>
HDOJ 4749 Parade Show
查看>>
python 多线程并发threading & 任务队列Queue
查看>>
【黑马程序员】资深程序员的见解
查看>>
1_fbauto
查看>>
IO体系、集合体系、多线程、jdbc
查看>>
Spring Cloud Turbine微服务集群实时监控
查看>>
django过滤富文本XSS
查看>>
Java异常处理机制及两种异常的区别
查看>>
【前端_js】最实用的正则表达式整理
查看>>
C#当中的扩展方法
查看>>
SharePoint 2013中PerformancePoint仪表板设计器连接Analysis Services 2012的问题
查看>>
基因组变异检测概述
查看>>
18个最佳代码编辑器
查看>>
通用所有数据库的插件,按键TC易语言VB,VC,DELPHI调用 ACTIVEX DLL
查看>>
HTTP状态码的类别
查看>>
android TextView、EditView部分字体特殊处理
查看>>