c 菜鸟教程,c语言

  • 编辑: 梁丘又香
  • 2019-11-16 15:04:28
  • 人阅读
c语言程序100例,在此期间,结果是。NetCore不基于IIS,我们的过去网络请求代码为。在NetCore框架下,存在不兼容和错误的可能性。这里没有明确的介绍。如何在NetCore下进行HTTP请求。c语言

在这期间,由于.net core 并不基于IIS,我们的过去的网络请求代码在.net core框架下,有可能会出现不兼容,报错的现象。这里大致介绍下在.net core 下如何进行http请求,主要仍然是GET和POST方法,有错误的地方,欢迎指正!

先来说POST,POST我实现了三种方法,前两种基于的原理是完全一致的,后面的有些小小的差异,但他们的本质都是http请求,本质上是无区别的,只是实现方法有所不同。

废话不多说,上代码:

POST异步方法:

/// summary/// 异步请求post(键值对形式,可等待的)/// /summary/// param name=uri网络基址(:59315)/param/// param name=url网络的地址(/api/UMeng)/param/// param name=formData键值对ListKeyValuePairstring, string formData = new ListKeyValuePairstring, string();formData.Add(new KeyValuePairstring, string(userid, 29122));formData.Add(new KeyValuePairstring, string(umengids, 29122));/param/// param name=charset编码格式/param/// param name=mediaType头媒体类型/param/// returns/returnspublic async Taskstring HttpPostAsync(string uri, string url, ListKeyValuePairstring, string formData = null, string charset = UTF-8, string mediaType = application/x-www-form-urlencoded){string tokenUri = url;var client = new HttpClient();client.BaseAddress = new Uri(uri);HttpContent content = new FormUrlEncodedContent(formData);content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);content.Headers.ContentType.CharSet = charset;for (int i = 0; i formData.Count; i ){content.Headers.Add(formData[i].Key, formData[i].Value);}HttpResponseMessage resp = await client.PostAsync(tokenUri, content);resp.EnsureSuccessStatusCode();string token = await resp.Content.ReadAsStringAsync();return token;}

POST同步方法:

/// summary/// 同步请求post(键值对形式)/// /summary/// param name=uri网络基址(:59315)/param/// param name=url网络的地址(/api/UMeng)/param/// param name=formData键值对ListKeyValuePairstring, string formData = new ListKeyValuePairstring, string();formData.Add(new KeyValuePairstring, string(userid, 29122));formData.Add(new KeyValuePairstring, string(umengids, 29122));/param/// param name=charset编码格式/param/// param name=mediaType头媒体类型/param/// returns/returnspublic string HttpPost(string uri, string url, ListKeyValuePairstring, string formData = null, string charset = UTF-8, string mediaType = application/x-www-form-urlencoded){string tokenUri = url;var client = new HttpClient();client.BaseAddress = new Uri(uri);HttpContent content = new FormUrlEncodedContent(formData);content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);content.Headers.ContentType.CharSet = charset;for (int i = 0; i formData.Count; i ){content.Headers.Add(formData[i].Key, formData[i].Value);}var res = client.PostAsync(tokenUri, content);res.Wait();HttpResponseMessage resp = res.Result;var res2 = resp.Content.ReadAsStringAsync();res2.Wait();string token = res2.Result;return token;}

遗憾的是,同步方法也是基于异步实现的,个人认为这样做会加大系统开销。如果各位有其他的高效实现,请不吝赐教!

接下来是通过流的方式进行POST:

public string Post(string url, string data, Encoding encoding, int type){try{HttpWebRequest req = WebRequest.CreateHttp(new Uri(url));if (type == 1){req.ContentType = application/json;charset=utf-8;}else if (type == 2){req.ContentType = application/xml;charset=utf-8;}else{req.ContentType = application/x-www-form-urlencoded;charset=utf-8;}req.Method = POST;//req.Accept = text/xml,text/javascript;req.ContinueTimeout = 60000;byte[] postData = encoding.GetBytes(data);Stream reqStream = req.GetRequestStreamAsync().Result;reqStream.Write(postData, 0, postData.Length);reqStream.Dispose();var rsp = (HttpWebResponse)req.GetResponseAsync().Result;var result = GetResponseAsString(rsp, encoding);return result;}catch (Exception ex){throw;}}

private string GetResponseAsString(HttpWebResponse rsp, Encoding encoding){Stream stream = null;StreamReader reader = null;try{// 以字符流的方式读取HTTP响应stream = rsp.GetResponseStream();reader = new StreamReader(stream, encoding);return reader.ReadToEnd();}finally{// 释放资源if (reader != null) reader.Dispose();if (stream != null) stream.Dispose();if (rsp != null) rsp.Dispose();}}

本文原标题:c 标准库,c语言可以做什么软件

版权声明:本文由 梁丘又香 整理编辑,如果侵权请联系我们!

转载注明出处:https://www.nintaus.net/houtai@123/makehtml_archives_action.php?endid=0&startid=0&typeid=0&totalnum=11732&startdd=5360&pagesize=20&seltime=0&sstime=1589370344&stime=&etime=&uptype=mkall&mkvalue=0&isremote=0&serviterm=

文章评论

共有 条评论来说两句吧...

用户名:

验证码:

Top