.net Json Web服务返回空响应
我创建了一个web服务,它从其他web服务获取数据并返回字符串响应,但当我第一次调用它时,它返回null,但第二次它工作得很好。 这是我的密码:.net Json Web服务返回空响应,.net,json,.net,Json,我创建了一个web服务,它从其他web服务获取数据并返回字符串响应,但当我第一次调用它时,它返回null,但第二次它工作得很好。 这是我的密码: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getStandings(string comp_id) { WebClient w = new WebClient(); w.Downlo
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getStandings(string comp_id)
{
WebClient w = new WebClient();
w.DownloadStringCompleted += StandingsDownloadCompleted;
w.DownloadStringAsync(new Uri("http://football-api.com/api/?Action=standings&APIKey=" + apikey + "&comp_id=" + comp_id));
return StandingsString;
}
public static string StandingsString { get; set; }
public void StandingsDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
StandingsString = e.Result;
}
有什么问题吗?w.DownloadStringAsync是异步的。在您有机会填充字符串之前第一次返回该字符串。在返回之前,您需要等待结果。或者使用同步下载字符串。感谢您的快速回答,我如何同步下载字符串?添加WebClient.DownloadString后,它不工作。DownloadString不会返回字符串。