我有2个功能
public void sendGETRequest()
{
System.Uri targetUri = new System.Uri(urlAdress);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.Method = "GET";
request.Headers["X_USERNAME"] = username;
request.Headers["X_PASSWORD"] = password;
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
result = httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
if (Completed != null)
Completed();
}
}
当我打电话
GET user;
public String GETUserList()
{
user = new GET();
user.urlAdress = url + "user";
user.username = username;
user.password = password;
user.sendGETRequest();
user.Completed += OutUser;
if (Completed != null)
Completed();
return user.result;
}
void OutUser()
{
Debug.WriteLine(user.result);
}
如果我像这样调用 1 个操作来获取用户列表
String json = GETUserList();
Debug.WriteLine("Hllllllllllll" + json + "HHHHHHHHHHHHHH" + p.resultWords);
它返回
HllllllllllllHHHHHHHHHHHHHH
[{"id":"3","username":"a","password":"a"},{"id":"4","username":"bbbb\n","password":"aaa"},{,{"id":"13","username":"abcdef","password":"abcdef"},{"id{"id":"10","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"11","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"12","word":"asdas","definition":"asdasd","cardset_id":"1"},{"id":"13","word":"asdas","definition":"asdasd","cardset_id":"1"},{"id":"14","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"15","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"16","word":"asdas","definition":"asdasd","
我认为函数 ReadWebRequestCallback(IAsyncResult callbackResult)是在后台运行的,当我调用以获取用户列表时, 它会在此之后执行(我已经调试过。如何在操作完成之前运行它?谢谢提前!