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(@"<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
原文:[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)
- 前言
- UWP win10 app 新關鍵字x:Bing
- win10應用 UWP 使用MD5算法
- win10 UWP讀寫文件
- UWP appButtonBar樣式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延遲加載元素
- UWP xaml 圓形頭像
- UWP 繪制圖形
- win10 uwp 通知Toast
- win10 UWP 顯示地圖
- win10 uwp 參考
- win10 uwp clone
- win10 uwp 裝機必備應用 含源代碼
- RichEditBox 使用自定義菜單
- win10 UWP FlipView
- win10 UWP 獲取系統信息
- win10 UWP 申請微軟開發者
- win10 UWP button
- win10 UWP Markdown 含源代碼
- win10 UWP 應用設置
- win10 UWP 九幽數據分析
- win10 UWP 圓形等待
- win10 UWP 標題欄后退
- win10 UWP 單元測試
- win10 UWP 你寫我讀
- win10 UWP RSS閱讀器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 應用通信