如何修复'An invalid request URI was provided. The request URI must either be an absolute URI or BaseAdd



当我尝试使用 HttpClient.GetStringAsync 发出 GET 请求时,我的程序会爆炸。
这是错误消息:
An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.

假设根据用户选择的 IP,我的程序向其不同的变体发出请求,以了解哪一个可用。 例如: 用户选择"192.168.0.20",然后我的程序必须向"192.168.0.1, 192.168.0.2, ...">

private static readonly HttpClient client = new HttpClient();
try
{
string[] ips = cbxIps.SelectedValue.ToString().Split('.');
string ip = ips[0] + "." + ips[1] + "." + ips[2] + ".";
for (int i = 1; i < 255; i++)
{
var responseString = client.GetStringAsync(ip + i.ToString() + ":8080/alive");
if (responseString.Result == "200")
{
Servidor s = new Servidor(IPAddress.Parse(ip + i.ToString()), "Servidor " + i.ToString());
Servidor.Agregar(s);
}
}
Actualizar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

调用GetStringAsync 时,您需要在 IP 地址之前指定 HTTP 方案(http://或 https://(

例如

var responseString = client.GetStringAsync("https://" + ip + i.ToString() + ":8080/alive");

相关内容

最新更新