window phone WebClient和HttpWebRequest 并解决gb2312乱码问题
来源:互联网 发布:淘宝订单要上传身份证 编辑:程序博客网 时间:2024/06/02 05:29
1.WebClient类
在System.Net空间下,提供向URI标识的目标发送数据和从URI标识的目标接收数据的类。
获取数据通过OpenReadAync,完成时OpenReadCompleted
private void DoWebClient()
{
string url = "http://www.qq.com";
WebClient client = new WebClient();
client.OpenReadAsync(new Uri(url));
client.OpenReadCompleted += new OpenReadCompletedEventHandler(WebClient_OpenReadCompleted);
}
private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (StreamReader reader = new StreamReader(e.Result,DBCSCodePage.DBCSEncoding.GetDBCSEncoding("gb2312")))
{
string contents = reader.ReadToEnd();
int begin = contents.ToString().IndexOf("<title>");
int end = contents.ToString().IndexOf("</title>");
//UI线程调用,直接更新界面
ClientText.Text = contents.ToString().Substring(begin + 7, end - begin - 7);
}
}
2.HttpWebRequest 类
在System.Net空间下,提供WebRequest类的HTTP的实现
private void DoHttpWebRequest()
{
string url = "http://www.baidu.com";
WebRequest request = HttpWebRequest.Create(url);
IAsyncResult result = request.BeginGetResponse(ResponseCallBack, request);
}
private void ResponseCallBack(IAsyncResult result)
{
HttpWebRequest req = result.AsyncState as HttpWebRequest;
WebResponse res = req.EndGetResponse(result);
using(Stream stream=res.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, DBCSCodePage.DBCSEncoding.GetDBCSEncoding("gb2312")))
{
string contents = reader.ReadToEnd();
int begin = contents.ToString().IndexOf("<title>");
int end = contents.ToString().IndexOf("</title>");
//基于后台运行,不在UI线程
Dispatcher.BeginInvoke(() => { ReqText.Text = contents.ToString().Substring(begin + 7, end - begin - 7); });
}
}
ps:DBCSCodePage就是用来解决乱码问题的
源码:http://download.csdn.net/detail/wulongtiantang/5072852
- window phone WebClient和HttpWebRequest 并解决gb2312乱码问题
- WebClient和HttpWebRequest区别
- 用HttpWebRequest代替WebClient解决POST超时问题
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- WebClient 和 HttpWebRequest的区别
- Windows Phone 7 网络编程之webclient和httpwebrequest的使用
- 解决Sublime Text 2乱码问题 支持GB2312和GBK
- httpWebRequest WebClient下载慢的问题处理
- 如何解决AJAX gb2312 乱码问题
- 解决javamail,接收gb2312乱码问题
- windows phone中解决html乱码问题
- Subline-Text2解决乱码,并支持GB2312与GBK
- Sublime Text 2支持GB2312和GBK,解决中文显示乱码问题
- Sublime Text 2支持GB2312和GBK,解决中文显示乱码问题
- Sublime Text 2支持GB2312和GBK,解决中文显示乱码问题
- Sublime Text 2支持GB2312和GBK,解决中文显示乱码问题
- OpenStack与KVM的区别与联系
- (转载)深度解析IOS开发中编码转换方法
- memcmp与strncmp函数
- 连接文件LINK
- 基于ActiveMQ 的发布/订阅(Pub/Sub) Chat 示例,上传了源码
- window phone WebClient和HttpWebRequest 并解决gb2312乱码问题
- android APK签名
- 三、第一个cocos2d程序的代码分析
- c++空指针赋初值
- Java序列化问题总结
- 中国 外包 2012(zz.IS2120@BG57IV3)
- py2exe使用
- 删除字符串开始及末尾的空白符,并且把数组中间的多个空格(如果有)
- ASP.NET高级配置