win10 UWP GET Post

最后更新于:2022-04-01 20:23:41

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进行正则 正则可以看 [正则快速](https://github.com/lindexi/lindexi_gd/blob/master/%E5%8D%9A%E5%AE%A2/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F30%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B.md) ~~~ Regex regex = new Regex(@"(\d\d\d人阅读)"); string str = regex.Match(content).Result("阅读:$1"); reminder(str); ~~~ 如果使用UI,直接使用会出现 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_57076374e0ab0.jpg) 我们写函数 ~~~ 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); }); ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_5707637502841.jpg) 我把之前写的一个刷500 cookie可以使用`HttpBaseProtocolFilter` 设置cookie ~~~ HttpCookie cookie = new HttpCookie("名称", "blog.csdn.net", "/") { Value = "a", }; filter.CookieManager.SetCookie(cookie, false); ~~~ 这写的不好,我将会写网络编程,这一篇会写容易的我的博客授权发在win10.me 原文:[http://www.cnblogs.com/linzheng/](http://www.cnblogs.com/linzheng/) 博客:[blog.csdn.net/lindexi_gd](http://blog.csdn.net/lindexi_gd/article/details/blog.csdn.net/lindexi_gd)
';