win10 应用应该是要有访问网络,网络现在最多的是使用GET,Post,简单的使用,可以用网络的数据:获得博客的访问量。
在使用网络,我们需要设置Package.appxmanifest
网络请求使用GET,首先有要访问的网站
string url = "/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)){//发送bytestring 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("名称", "", "/"){Value = "a",};filter.CookieManager.SetCookie(cookie, false);
这写的不好,我将会写网络编程,这一篇会写容易的我的博客授权发在win10.me
原文:/linzheng/
博客:/lindexi_gd