WP7与服务器的交互问题
请教下高手们
如果要和web服务器进行交互,发出的是http://....../Handler.ashx?id=xxx&pwd=xxx
怎样把这个发到服务端并接收服务端发回的消息?
[解决办法]
用Create创建一个HttpWebRequest对象
用BeginGetRespone开始对指定的URI资源做异步请求
用GetResponseStream获得响应的数据流
HttpWebRequest还有一些相应API 上MSDN上查下
[解决办法]
public void GetStringAsync() { Uri uri = new Uri(@"http://....../Handler.ashx?id=xxx&pwd=xxx"); WebRequest request= WebRequest.Create(sb.ToString()); //开始请求 request.BeginGetResponse(new AsyncCallback(GetStringComplete), request); } private void GetStringComplete(IAsyncResult asyncResult) { try { HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult); //得到响应流 Stream streamResult = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(streamResult); //获取的返回值 string resultString = reader.ReadToEnd(); Deployment.Current.Dispatcher.BeginInvoke(delegate() { //在ui中使用resultString }); } catch (WebException ex) { } }