WP7 http异步通信
来源:互联网 发布:太平洋软件网站首页 编辑:程序博客网 时间:2024/05/16 05:43
暑假
做wp7开发的过程中遇到了通信问题,尝试了很多方法来实现同步通信,比如线程之类的,但是始终是控制不住,没办法,只能是利用异步的方法来实现通信,代码奉上:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //url就是你要请求的网址
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = CommonHelper.postData;
postStream.Write(byteArray, 0, CommonHelper.postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
//收索的回调
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd(); //responsestring就是返回给你的数据
streamResponse.Close();
streamRead.Close();
response.Close();
Dispatcher.BeginInvoke(
() =>
{
在这里面写一些对界面进行更改的操作
});
}
个人博客:http://blog.amtemai.com
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //url就是你要请求的网址
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = CommonHelper.postData;
postStream.Write(byteArray, 0, CommonHelper.postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
//收索的回调
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd(); //responsestring就是返回给你的数据
streamResponse.Close();
streamRead.Close();
response.Close();
Dispatcher.BeginInvoke(
() =>
{
在这里面写一些对界面进行更改的操作
});
}
个人博客:http://blog.amtemai.com
- WP7 http异步通信
- WP7的Http通信
- WP7 异步操作 HttpWebRequest
- WP7 - RestSharp + JSON Http Post
- HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理
- HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理
- 异步通信
- 异步通信
- kotlin 异步线程简单事例,加http请求,异步线程之间的通信
- wp7
- Http 通信
- http通信
- HTTP 通信
- Http通信
- Http通信
- Http通信
- HTTP 通信
- Http通信
- 时间管理
- 获取WP7手机的本机设备信息
- Oracle Flashback之Flashback database
- 南京出差
- maven eclipse 集成
- WP7 http异步通信
- 精彩博客链接
- QQ在线,Q我吧
- HDU 4082 Hou Yi's secret
- C#关于xml数据的解析
- Oracle Flashback之Flashback table和flashback drop
- 幸福,很简单
- 命名空间小结
- 使用sqlcmd命令