win10 UWP GET Post
win10 应用应该是要有访问网络,网络现在最多的是使用GET,Post,简单的使用,可以用网络的数据:获得博客的访问量。
在使用网络,我们需要设置Package.appxmanifest
网络请求使用GET,首先有要访问的网站
string url = "http://blog.csdn.net/lindexi_gd/article/details/50830924";
//url是我一篇博客,win10 UWP Hmac,我很多博客都是读书笔记
WebRequest是请求基类,需要使用WebRequest.Create(url);
request.Method = "GET";
UWP 的Header设置
request.Headers["Cookie"]
接受需要一个函数 AsyncCallback
private void response_callback(IAsyncResult result)
request.BeginGetResponse(response_callback, request);
response_callback接受信息HttpWebRequest http_web_request = (HttpWebRequest)result.AsyncState;
WebResponse web_response = http_web_request.EndGetResponse(result);
using (Stream stream = web_response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string content = reader.ReadToEnd();
}
}
我们需要对content进行正则
正则可以看 正则快速
Regex regex = new Regex(@"<span class=""link_view"" title=""阅读次数"">(\d\d\d人阅读)</span>");
string str = regex.Match(content).Result("阅读:$1");
reminder(str);
如果使用UI,直接使用会出现
我们写函数
private async void reminder(string str)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
tb.Text += str;
});
}
网络很容易就异常
catch (WebException e)
{
switch (e.Status)
{
case WebExceptionStatus.CacheEntryNotFound:
break;
case WebExceptionStatus.ConnectFailure:
reminder("ConnectFailure:远程服务器连接失败");
break;
case WebExceptionStatus.ConnectionClosed:
break;
case WebExceptionStatus.KeepAliveFailure:
break;
case WebExceptionStatus.MessageLengthLimitExceeded:
reminder("MessageLengthLimitExceeded 网络请求消息长度受到限制");
break;
case WebExceptionStatus.NameResolutionFailure:
break;
case WebExceptionStatus.Pending:
reminder("Pending 内部异步挂起");
break;
case WebExceptionStatus.PipelineFailure:
break;
case WebExceptionStatus.ProtocolError:
break;
case WebExceptionStatus.ProxyNameResolutionFailure:
break;
case WebExceptionStatus.ReceiveFailure:
break;
case WebExceptionStatus.RequestCanceled:
break;
case WebExceptionStatus.RequestProhibitedByCachePolicy:
break;
case WebExceptionStatus.RequestProhibitedByProxy:
break;
case WebExceptionStatus.SecureChannelFailure:
break;
case WebExceptionStatus.SendFailure:
break;
case WebExceptionStatus.ServerProtocolViolation:
break;
case WebExceptionStatus.Success:
break;
case WebExceptionStatus.Timeout:
break;
case WebExceptionStatus.TrustFailure:
break;
case WebExceptionStatus.UnknownError:
break;
}
reminder(e.Message);
}
post需要把request.Method = "POST";
传输在request.BeginGetRequestStream(respeonse_streamCallback, request);
private void respeonse_streamCallback(IAsyncResult result)
{
HttpWebRequest http_web_request = (HttpWebRequest) result.AsyncState;
using (Stream stream=http_web_request.EndGetRequestStream(result))
{
//发送byte
string str = "c";
byte[] buffer = Encoding.UTF8.GetBytes(str);
stream.Write(buffer,0,buffer.Length);
}
http_web_request.BeginGetResponse(response_callback, http_web_request);
}
简单方法
HttpClient http=new HttpClient();
reminder(await http.GetStringAsync(new Uri(url)));
获整个对象
HttpResponseMessage response = await http.GetAsync(new Uri(url));
reminder(await response.Content.ReadAsStringAsync());
HttpClient http = new HttpClient();
HttpStringContent http_string =new HttpStringContent("a");
HttpResponseMessage response = await http.PostAsync(new Uri(url), http_string);
string str = await response.Content.ReadAsStringAsync();
reminder(str);
HttpClient http = new HttpClient();
InMemoryRandomAccessStream memory =new InMemoryRandomAccessStream();
HttpStreamContent stream=new HttpStreamContent(memory);
HttpResponseMessage response = await http.PostAsync(new Uri(url), stream);
string str = await response.Content.ReadAsStringAsync();
reminder(str);
HttpClient http = new HttpClient();
InMemoryRandomAccessStream memory = new InMemoryRandomAccessStream();
HttpStreamContent stream = new HttpStreamContent(memory);
HttpRequestMessage request=new HttpRequestMessage(HttpMethod.Post,new Uri(url));
request.Content = stream;
HttpResponseMessage response = await http.SendRequestAsync(request);
string str = await response.Content.ReadAsStringAsync();
看到有人说CSDN博客访问统计是Cache,如果我们要有很多访问,可以使用
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;
await Task.Run(() =>
{
reminder("\n");
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Headers["Cookie"] = string.Empty;
request.BeginGetResponse(response_callback, request);
});
我把之前写的一个刷500
cookie可以使用HttpBaseProtocolFilter
设置cookie
HttpCookie cookie = new HttpCookie("名称", "blog.csdn.net", "/")
{
Value = "a",
};
filter.CookieManager.SetCookie(cookie, false);
这写的不好,我将会写网络编程,这一篇会写容易的我的博客授权发在win10.me
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理