public class A{
public void SetJsonSource(string _Uri)
{
WebRequest req = WebRequest.Create(_Uri);
req.Method = "GET";
req.BeginGetResponse(ResponseCallback, req);
}
private void ResponseCallback(IAsyncResult result)
{
string JsonSource = "";
HttpWebRequest httpwenReq = (HttpWebRequest)result.AsyncState;
WebResponse webResponse = httpwenReq.EndGetResponse(result);
using (Stream stream = webResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
JsonSource = reader.ReadToEnd();
}
}
}
public class B{
void GetSource(){
A a =new A();
a.SetJsonSource("****");
}
}
問題是怎麼樣得到ResponseCallback裡面的JsonSource?
定義一個委托或函數,然後在ResponseCallback調用就可以了