提供了无效的请求URI.请求URI必须是绝对URI或必须设置BaseAddress

Ehs*_*bar 13 c# http httpclient

我试图使用此代码从网页获取内容:

HttpClient http = new HttpClient();
  var response = await http.GetByteArrayAsync("www.nsfund.ir/news?p_p_id=56_INSTANCE_tVzMoLp4zfGh&_56_INSTANCE_tVzMoLp4zfGh_mode=news&_56_INSTANCE_tVzMoLp4zfGh_newsId=3135919&p_p_state=maximized");
   String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
   source = WebUtility.HtmlDecode(source);
   HtmlDocument resultat = new HtmlDocument();
   resultat.LoadHtml(source);
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

提供了无效的请求URI.请求URI必须是绝对URI或必须设置BaseAddress.

Yac*_*sad 14

您只需要指定完整的URL(包括协议),如下所示:

var response = await http.GetByteArrayAsync("http://www.nsfund.ir/news?p_....
Run Code Online (Sandbox Code Playgroud)


Nie*_*jes 7

绝对URI遵循protocol://server/path?query#hash约定.由于您没有明确指定协议,http://或者https://在您的情况下,URL不是绝对的,因此无法解析.