Silverlight 数据通讯

来源:互联网 时间:1970-01-01

WebClient:

void Async(object sender, SelectionChangedEventArgs e)  {        Uri endpoint = new Uri(String.Format("http://localhost:port/XXXXX.ashx?parameter={0}",value));
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(endpoint);
    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)   {
//处理返回的数据

    }

 

WebRequest :

void Request(object sender, SelectionChangedEventArgs e)
{
   Uri endpoint = new Uri(http://localhost:port/XXXXXXX.ashx);
    WebRequest request = WebRequest.Create(endpoint);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
    request.BeginGetResponse(new AsyncCallback(ResponseReady), request); 
}

// 将数据(查询数据的条件)写入请求流中。

void RequestReady(IAsyncResult asyncResult) { 

WebRequest request = asyncResult.AsyncState as WebRequest; 

Stream requestStream = request.EndGetRequestStream(asyncResult); 

using (StreamWriter writer = new StreamWriter(requestStream)) 

{ writer.Write(String.Format("parameter={0}", value)); writer.Flush(); }

}

// 响应,处理取得的数据

void ResponseReady(IAsyncResult asyncResult) {

 WebRequest request = asyncResult.AsyncState as WebRequest; 

WebResponse response = request.EndGetResponse(asyncResult); 

using (Stream responseStream = response.GetResponseStream()) 

{ StreamReader reader = new StreamReader(responseStream);

 var reponsedata = reader.ReadToEnd();//响应的数据

 }

}

 

WCF:

TODO

ASMX:

请求

            WebServiceReference.WebServiceSoapClient client = new WebServiceReference.WebServiceSoapClient();
            client.HelloWorldCompleted += client_HelloWorldCompleted;
            client.HelloWorldAsync();

 

响应

          void client_HelloWorldCompleted(object sender, WebServiceReference.HelloWorldCompletedEventArgs e)
        {

        }

本文来自食品中化学扫盲的博客,原文地址:http://hi.baidu.com/%CA%B3%C6%B7%D6%D0%BB%AF%D1%A7%C9%A8%C3%A4/blog/item/429e9916e09937184a90a7fd.html


相关阅读:
Top