如果要和web服务器进行交互,发出的是http:// ....../Handler.ashx?id=xxx&pwd=xxx
怎样把这个发到服务端并接收服务端发回的消息?
用HttpWebRequest发请求
用GetResponseStream获得响应的数据流
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)
{
}
}