我通过以下方式使用需要 Bacis-Authentication 的 REST-Services:
private void button1_Click(object sender, EventArgs e) {
try {
var url = "http://172.34.1.111:8088/ustrich/rest/projects/pid/1234";
using (var wc = this.CreateWebClient(url)) {
var stopWatch = new Stopwatch();
stopWatch.Start();
var s = wc.DownloadString(url);
stopWatch.Stop();
this.label1.Text = stopWatch.Elapsed.ToString();
this.textBox1.Text = s;
}
} catch (Exception ex) {
this.textBox1.Text = ex.ToString();
}
}
protected WebClient CreateWebClient(string url) {
var webClient = new WebClient();
var cache = new CredentialCache { { new Uri(url), "Basic", new NetworkCredential("rest", "rest") } };
webClient.Credentials = cache;
webClient.Headers.Add("Content-Type", "text/xml; charset=utf-8");
webClient.Encoding = Encoding.UTF8;
return webClient;
}
现在的问题是,当我启动我的测试应用程序并执行此代码时,第一次在 wc.DownloadString(url) 上大约需要 5 秒。以下调用,当我点击按钮上的第二次等时间时,只需要 300 毫秒。现在我的问题是:我的代码有问题吗?我可以在那里优化一些东西来解决第一次通话需要 5 秒的问题吗?谢谢你的帮助。