基本技术点 自己记录用于复习
开头顺便记下 解析 xml的 正则表达式
XElement element = XElement.Parse(s);
OutputList.ItemsSource = (from c in element.Elements("item") select c.Attribute("name").Value );很简单 from 和select 语句 很方便
windows 8 中 使用 httpclient 代替 windows phone 中的 httpwebclient , 使用方法 也有些不同 。
下面是windows 8种使用 httpclient的各种方式
a 以get的方式 对服务器发起请求 获得string
HttpResponseMessage response = await httpClient.GetAsync(resourceUri); // await关键字 异步加载 * resourceUri 是请求的Url
string statues_string = ((int)response.StatusCode) + " " + response.ReasonPhrase + Environment.NewLine; // 服务器返回 请求的 状态
responseBodyAsText = await response.Content.ReadAsStringAsync(); //将 请求返回的 柳 转化为 string
b 以get的方式 对服务器发去请求 接受返回来的流
string resourceAddress = ";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, resourceAddress);
HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
OutputField.Text = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;
StringBuilder responseBody = new StringBuilder();
using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
byte[] responseBytes = new byte[responseStream.Length];
read = await responseStream.ReadAsync(responseBytes, 0, responseBytes.Length);}
c 以post的方式 上传 string
HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, new StringContent(RequestBodyField.Text));
catch (HttpRequestException hre) { rootPage.NotifyUser("Error", NotifyType.ErrorMessage); OutputField.Text = hre.ToString(); }
catch (TaskCanceledException) { rootPage.NotifyUser("Request canceled.", NotifyType.ErrorMessage); }
finally { Helpers.ScenarioCompleted(StartButton, CancelButton); } //各种异常处理
d 以post的方式 上传流数据
byte[] subData = new byte[size];
for (int i = 0; i < subData.Length; i++)
{ subData[i] = 64; }
Stream stream=(Stream)new MemoryStream(subData);
StreamContent streamContent = new StreamContent(stream);
string resourceAddress =“http://www.baidu.com”;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, resourceAddress);request.Content = streamContent;
request.Headers.TransferEncodingChunked = true; // 假设我们不知道内容的长度HttpResponseMessage response = await httpClient.SendAsync(request); //发送数据
若消息返回来后 数据都在response中