我正在尝试使用HttpClient获取页面的URL。我以前只使用HttpWebRequest,但是我需要使它成为异步方法。在下面的代码中,myUri始终返回null,当我稍后尝试处理它时,将引发异常。
位置标头使用的是错误的东西吗?
string myUrl = "http://www.example.com/";
Uri myUri= new Uri(myUrl);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(myUri))
{
if (response.IsSuccessStatusCode)
{
myUri= response.Headers.Location;
Debug.WriteLine("True "+ myUri);
}
else {
Debug.WriteLine("False " + myUri);
}
}
Run Code Online (Sandbox Code Playgroud)