httpwebrequest 给出错误的结果


 if (IsURLValid(url))
            {
                Uri requestUri = new Uri(url);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
                httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();
                httpWebRequest.Timeout = 3000;
                httpWebRequest.Method = "HEAD";
                //httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0";
                httpWebRequest.Accept = "*/*";
                httpWebRequest.ServicePoint.Expect100Continue = false;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.MaximumAutomaticRedirections = 4;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                httpWebResponse.Close();
                MessageBox.Show(httpWebResponse.ResponseUri.ToString());
                if ((httpWebResponse.StatusCode == HttpStatusCode.OK) || (httpWebResponse.StatusCode == HttpStatusCode.Redirect))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
        catch (WebException)
        {
            return false;
        }
    }

上面的代码运行良好,但它给出了一些错误的结果。下面的网址是实时的,但是我们得到的结果是无效的。

https://forum.unity.com/threads/clean-way-to-talk-to-external-application-c-to-ros-c-node.418849/

我也尝试过在线检查器,这些检查器也给出了相同的错误。https://httpstatus.io/检查 Httpstatus 中的 Unity URL 其给出错误。有人知道为什么会这样吗?

你可以试试这个:

httpWebRequest.CookieContainer = new CookieContainer();

否则,您的(HttpWebResponse)httpWebRequest.GetResponse();将引发异常,您将从 catch 语句中获取return false;

有关更多信息,请参阅此处

相关内容

  • 没有找到相关文章

最新更新